c - malloc causing segmentation fault by _int_malloc -
i have tree construction adding big amount of nodes too. number of times done (tree cleaned between runs) , number of nodes given command line argument. numbers of nodes < 6000 , number of runs programme performs expected. when number of nodes exceeds , number of runs exceeds low number around 50 programme causes segmentation fault.
programme received signal sigsegv, segmentation fault. _int_malloc (av=0x7ffff7201740 <main_arena>, bytes=112) @ malloc.c:3570 3570 malloc.c: no such file or directory.
using backtrace tracks too
#0 _int_malloc (av=0x7ffff7201740 <main_arena>, bytes=112) @ malloc.c:3570 #1 0x00007ffff6ecbfb5 in __gi___libc_malloc (bytes=112) @ malloc.c:2924 #2 0x0000000000401a99 in createtreeforquad (quad=...) @ cs257.c:217 #3 0x0000000000401b3a in addquadstotree (tree=tree@entry=0x2f965c8) @ cs257.c:230 #4 0x0000000000401dec in addbody (tree=tree@entry=0x2f965c8, body=...) @ cs257.c:292 #5 0x0000000000402146 in addbodytocorrectquad (body=..., tree=tree@entry=0x2f961c8) @ cs257.c:245 #6 0x0000000000401eaf in addbody (tree=tree@entry=0x2f961c8, body=...) @ cs257.c:296 #7 0x0000000000402146 in addbodytocorrectquad (body=..., tree=tree@entry=0x2f95dc8) @ cs257.c:245
note addbody -> addbodytocorrectquad -> addbody recursion happens big number of times @ high number of nodes. code malloc fails below.
tree *createtreeforquad(quad quad) { tree *tree; tree = (tree *)malloc(sizeof*tree); if (tree != null){ tree->quad = quad; tree->internal = 0; tree->bodyempty = 1; homecoming tree; }else{ printf("\n ------------------------------------ malloc failed----------------------------------------"); } }
the code utilize free tree follows, beingness called on root node , internal flag beingness set 0 when tree leaf.
void cleantree(tree **tree) { if((*tree)->internal == 0) { free(*tree); } else{ cleantree(&((*tree)->ne)); cleantree(&((*tree)->se)); cleantree(&((*tree)->sw)); cleantree(&((*tree)->nw)); cleantree(&((*tree)->ne1)); cleantree(&((*tree)->nw1)); cleantree(&((*tree)->se1)); cleantree(&((*tree)->sw1)); free(*tree); } }
the tree struct looks
typedef struct tree tree; struct tree { body body; quad quad; tree *ne; tree *nw; tree *se; tree *sw; tree *ne1; tree *nw1; tree *se1; tree *sw1; int internal; int bodyempty; };
the code adding bodys tree follows addbodytocorrectquad
calling addbody
on quad body exists within.
void addbody(tree **tree, body body) { if( (*tree)->bodyempty == 1) { (*tree)->body = body; (*tree)->bodyempty = 0; } else { if((*tree)->internal) { (*tree)->body = combinebody((*tree)->body, body); addbodytocorrectquad(body, tree); //printf("b\n"); } else{ (*tree)->internal = 1; / addquadstotree(tree); //printf("%f",((*tree)->nw)->quad.x); addbodytocorrectquad((*tree)->body, tree); (*tree)->body = combinebody((*tree)->body, body); addbodytocorrectquad(body, tree); //printf("c\n"); } } }
you have heap corruption somewhere -- running off end of array or dereferencing invalid pointer or using object after has been freed.
try using valgrind or other memory debugging tool narrow downwards problem is.
c segmentation-fault malloc
No comments:
Post a Comment