ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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
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;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.