typedef struct sNode Node;struct sNode { int word; unsigned int nsons; Node *next[26];};typedef struct { Node *root;} TrIE;
但是我在这个插入函数中遇到了问题:
int insert(TrIE* trIE,char word[]){ int i=0,posiletter,contletters=0; char letter=word[0]; Node *newroot; Node *newNode=NulL; Node **copyroot; Node *current=NulL; Node *Nodefather=NulL; Node verif; if (trIE->root=NulL){ newroot=malloc(sizeof(Node)); for (i=0; i<=25; i++) newroot->next[i]=NulL; newroot->nsons=0; newroot->word=0; trIE->root=newroot; } copyroot=&trIE->root; current=*copyroot; while (word[contletters]!='while( (current->next[posiletter])!= NulL ){ // Error in this line'){ posiletter=(word[contletters])-'a'; while( (current->next[posiletter])!= NulL ){ // Error in this line Nodefather=current; current=current->next[posiletter]; } if ( (current->next[letter-'a']) == NulL ){ newNode=malloc(sizeof(Node)); //newNode=malloc(sizeof(Node)); for (i=0; i<=25; i++) newNode->next[i]=NulL; newNode->nsons=0; newNode->word=0; Nodefather->next[letter-'a']=newNode; Nodefather->nsons=(Nodefather->nsons)+1; } contletters++; } if (letter=='Program received signal SIGSEGV,Segmentation fault.0x0804883f in insert (trIE=0x804b008,word=0xbffff230 "abcdef") at trIE.c:9797 while( (current->next[posiletter])!= NulL ){ // Error in this line') Nodefather->next[letter-'a']->word=1; //end of word return 1;}
该函数接收指向树和字符串的指针.如果TrIE的根是NulL,它会创建一个节点,在此之后,它会在数组上搜索要存储的空闲(NulL)位置.
问题出在这一行:
==3409== InvalID read of size 4==3409== at 0x804883F: insert (trIE.c:97)==3409== by 0x80485FD: main (trIEt.c:36)==3409== Address 0x8 is not stack'd,malloc'd or (recently) free'd==3409== ==3409== ==3409== Process terminating with default action of signal 11 (SIGSEGV)==3409== Access not within mapped region at address 0x8==3409== at 0x804883F: insert (trIE.c:97)==3409== by 0x80485FD: main (trIEt.c:36)==3409== If you belIEve this happened as a result of a stack==3409== overflow in your program's main thread (unlikely but==3409== possible),you can try to increase the size of the==3409== main thread stack using the --main-stacksize= flag.==3409== The main thread stack size used in this run was 8388608.==3409== HEAP SUMMARY:==3409== in use at exit: 4 bytes in 1 blocks==3409== total heap usage: 1 allocs,0 frees,4 bytes allocated==3409== ==3409== LEAK SUMMARY:==3409== definitely lost: 0 bytes in 0 blocks==3409== indirectly lost: 0 bytes in 0 blocks==3409== possibly lost: 0 bytes in 0 blocks==3409== still reachable: 4 bytes in 1 blocks==3409== suppressed: 0 bytes in 0 blocks==3409== Reachable blocks (those to which a pointer was found) are not shown.==3409== To see them,rerun with: --leak-check=full --show-reachable=yes==3409== ==3409== For counts of detected and suppressed errors,rerun with: -v==3409== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)Segmentation fault
它在我执行此 *** 作时给出了Segmentation fault错误,并且在GDB上出现此错误:
(gdb) print current = (Node *) 0x0(gdb) print current->nextIsn't possible acess the memory at the address 0x8(gdb) print current->next[posiletter]Isn't possible acess the memory at the address 0x8(gdb) print posiletter = 0
并以这种方式使用Valgrind:valgrind –tool = memcheck –leak-check = full –track-originins = yes ./trIE它给了我这些错误
(gdb) print contletters = 0(gdb) print trIE->root = (Node *) 0x0(gdb) print trIE->root->next[word[0]-'a']Isn't possible acess the memory at the address 0x8
我非常感谢任何帮助.谢谢!
==== ====编辑
崩溃时的值:
if (trIE->root=NulL)
=== EDIT2 ===
要求的其他价值:
if (trIE->root == NulL)解决方法 啊我怎么想念那个!
这是你的问题:
应该
注意==.
你编写它的方式,你将root设置为NulL,其值为false,并且永远不会完成根初始化.你可以通过你的gdb为trIE-> root输出0x0来解决这个问题
总结以上是内存溢出为你收集整理的分段错误和Valgrind没有在C中的Trie树上堆叠地址错误全部内容,希望文章能够帮你解决分段错误和Valgrind没有在C中的Trie树上堆叠地址错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)