LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2014, 03:59 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Function calling itself seg faults


I am stuck with this function that loops to itself and I keep getting a seg fault at the line of the function calling itself.
This is a nested function instide another function and it uses a
local variable. Is there any way to do this ill mark the function call with exclamation marks.

Code:
bool loop_parse_room (short int x, short int y, room_rnum parse_room)
{
     bool cont = FALSE;

     short int dir;

     if ( parse_room != 0x00 )
     {
          for ( dir = 0; dir < NUM_DIR; dir++ )
          {
               if ( dir == UP || dir == DOWN || dir == INDIR || dir == OUTDIR )
                    continue;

               if ( world[parse_room].dir_option[dir] != NULL )
               {
                         if ( dir == NORTH     )  {y--;     }
                    else if ( dir == EAST      )  {x++;     }
                    else if ( dir == SOUTH     )  {y++;     }
                    else if ( dir == WEST      )  {x--;     }
                    else if ( dir == NORTHWEST )  {x--; y--;}
                    else if ( dir == NORTHEAST )  {y--; x++;}
                    else if ( dir == SOUTHEAST )  {y++; x++;}
                    else if ( dir == SOUTHWEST )  {x--; y++;}

                    if ( x >= ch->map_width ) { x--; cont = TRUE; }
                    if ( x < 0 ) { x++; cont = TRUE; }
                    if ( y >= ch->map_height ) { y--; cont = TRUE; }
                    if ( y < 0 ) { y++; cont = TRUE; }

                    if ( cont == TRUE ) { cont = FALSE; continue; }

                    map[y][x] = world[parse_room].dir_option[dir]->to_room;
                    !!!!!!!!!loop_parse_room(x, y, world[parse_room].dir_option[dir]->to_room);
               }

               // world[world[IN_ROOM(ch)].dir_option[dir]->to_room].dir_option[dir]
          }

          return TRUE;
     }
     else
          return FALSE;
}
 
Old 08-01-2014, 05:24 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Are you exceeding the available stack size? Can you show a backtrace of a core dump?
 
Old 08-01-2014, 09:53 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What this function should do? (Other than infinite recursion.)
 
Old 08-03-2014, 03:55 PM   #4
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
I wouldn't call it infinit it should return after all the rooms in a square have been checked
for exits. It should start from the center and check every room with a maximum size
height of five and a width of nine. Errors below I don't think it's a stack overflow.

Code:
==7018== Stack overflow in thread 1: can't grow stack to 0xbe2c6ffc
==7018== 
==7018== Process terminating with default action of signal 11 (SIGSEGV)
==7018==  Access not within mapped region at address 0xBE2C6FFC
==7018==    at 0x804D283: loop_parse_room.6925 (act.informative.c:3391)
==7018==  If you believe this happened as a result of a stack
==7018==  overflow in your program's main thread (unlikely but
==7018==  possible), you can try to increase the size of the
==7018==  main thread stack using the --main-stacksize= flag.
==7018==  The main thread stack size used in this run was 8388608.
==7018== Stack overflow in thread 1: can't grow stack to 0xbe2c6fdc
==7018== 
==7018== Process terminating with default action of signal 11 (SIGSEGV)
==7018==  Access not within mapped region at address 0xBE2C6FDC
==7018==    at 0x4022510: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-x86-linux.so)
==7018==  If you believe this happened as a result of a stack
==7018==  overflow in your program's main thread (unlikely but
==7018==  possible), you can try to increase the size of the
==7018==  main thread stack using the --main-stacksize= flag.
==7018==  The main thread stack size used in this run was 8388608.
==7018== 
==7018== HEAP SUMMARY:
==7018==     in use at exit: 7,483,816 bytes in 33,201 blocks
==7018==   total heap usage: 34,129 allocs, 928 frees, 8,439,100 bytes allocated
==7018== 
==7018== LEAK SUMMARY:
==7018==    definitely lost: 40 bytes in 1 blocks
==7018==    indirectly lost: 64 bytes in 4 blocks
==7018==      possibly lost: 0 bytes in 0 blocks
==7018==    still reachable: 7,483,712 bytes in 33,196 blocks
==7018==         suppressed: 0 bytes in 0 blocks
==7018== Rerun with --leak-check=full to see details of leaked memory
==7018== 
==7018== For counts of detected and suppressed errors, rerun with: -v
==7018== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Segmentation fault
 
Old 08-03-2014, 04:38 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by errigour View Post
I wouldn't call it infinit it should return after all the rooms in a square have been checked
for exits.
If you have 2 adjacent rooms, room1 will have an exit to room2, and room2 will have an exit to room1. What will prevent your function from cycling between room1 and room2 endlessly?

PS get rid of the variable cont, instead of cont = TRUE; just use continue;
 
1 members found this post helpful.
Old 08-04-2014, 07:55 AM   #6
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Thanks for the help ntubski your right it will cycle endlessly I need another if statement.

Also I need the other if statements to cycle threw that's why there is a continue. after the if statements.

Last edited by errigour; 08-04-2014 at 08:06 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Make believe seg faults binarybob0001 Programming 2 12-10-2007 04:37 AM
GCC seg faults benne Linux - Software 5 09-05-2005 01:09 AM
Repeated seg faults Tick Linux - General 5 07-29-2004 07:09 PM
bash seg faults Kilka *BSD 4 12-15-2003 01:40 AM
w3m seg faults slakmagik Linux - Software 0 05-04-2003 10:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration