繁体   English   中英

使用valgrind测试时,C程序将节点添加到链表的末尾,从而面临无效的大小错误写入

[英]C program adding node to end of linkedlist facing invalid write of size error when testing with valgrind

我是c的新手,当时我正在编写一个将节点添加到链表的玩具程序,如下所示:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>


typedef struct myToken {
    char name[64];
    struct myToken *next;
} tokenList;

typedef struct myAnagram {
    char name[64];
    tokenList *firstToken;
    struct myAnagram *next;
} anagramList;


void addtoken (anagramList** cur_token, char *new_token){
    anagramList* new_node = (anagramList*)malloc(sizeof(anagramList*));
    if (new_node == NULL){
        fprintf(stderr, "Unable to allocate memory for new node\n");
        exit(1);
    }
    anagramList *end = *cur_token;
    strcpy(new_node -> name,new_token);
    new_node -> next = NULL;
    if (*cur_token == NULL){
      *cur_token = new_node;
    }else{
        while(1){
            if (end -> next == NULL){
                end -> next = new_node;
                break;
            }
            end = end -> next;

        }
    }
  }


void print_anagram (anagramList *cur_token){
    while (cur_token != NULL){
        printf("%s\n",cur_token -> name);
        cur_token = cur_token -> next;
    }

}

int main () {
  int counts[26];
  char str[65];
  anagramList* head = NULL;

  while (scanf("%64s",str) != EOF) {
        addtoken(&head,str);
  }
  print_anagram(head);

  return 0;
}

当我从stdin读取内容时,它可以正常工作并提供正确的输出,当我使用valgrind测试时,出现消息:

$ valgrind ./anagrams
==11722== Memcheck, a memory error detector
==11722== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11722== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==11722== Command: ./anagrams)
==11722== 
hello
==11722== Invalid write of size 8
==11722==    at 0x4007DC: addtoken (in anagrams)
==11722==    by 0x400893: main (in anagrams)
==11722==  Address 0x5200088 is 8 bytes before an unallocated block of size 4,194,128 in arena "client"
==11722== 
hi
==11722== Invalid read of size 8
==11722==    at 0x400801: addtoken (in anagrams)
==11722==    by 0x400893: main (in anagrams)
==11722==  Address 0x5200088 is 8 bytes before a block of size 8 alloc'd
==11722==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11722==    by 0x400786: addtoken (in anagrams)
==11722==    by 0x400893: main (in anagrams)
==11722== 
==11722== Invalid write of size 8
==11722==    at 0x400812: addtoken (in anagrams)
==11722==    by 0x400893: main (in anagrams)
==11722==  Address 0x5200088 is 8 bytes before a block of size 8 alloc'd
==11722==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11722==    by 0x400786: addtoken (in anagrams)
==11722==    by 0x400893: main (in anagrams)
==11722== 
ok
==11722== Invalid read of size 8
==11722==    at 0x40081C: addtoken (in anagrams)
==11722==    by 0x400893: main (in anagrams)
==11722==  Address 0x5200088 is 8 bytes before a block of size 8 alloc'd
==11722==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11722==    by 0x400786: addtoken (in anagrams)
==11722==    by 0x400893: main (in anagrams)
==11722== 
bye
you
hello
==11722== Invalid read of size 8
==11722==    at 0x400846: print_anagram (in anagrams)
==11722==    by 0x4008BD: main (in anagrams)
==11722==  Address 0x5200088 is 8 bytes before a block of size 8 alloc'd
==11722==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11722==    by 0x400786: addtoken (in anagrams)
==11722==    by 0x400893: main (in anagrams)
==11722== 
hi
ok
bye
you
==11722== 
==11722== HEAP SUMMARY:
==11722==     in use at exit: 40 bytes in 5 blocks
==11722==   total heap usage: 5 allocs, 0 frees, 40 bytes allocated
==11722== 
==11722== LEAK SUMMARY:
==11722==    definitely lost: 40 bytes in 5 blocks
==11722==    indirectly lost: 0 bytes in 0 blocks
==11722==      possibly lost: 0 bytes in 0 blocks
==11722==    still reachable: 0 bytes in 0 blocks
==11722==         suppressed: 0 bytes in 0 blocks
==11722== Rerun with --leak-check=full to see details of leaked memory
==11722== 
==11722== For counts of detected and suppressed errors, rerun with: -v
==11722== ERROR SUMMARY: 30 errors from 5 contexts (suppressed: 0 from 0)

我真的不知道为什么valgrind测试失败,并且我还没有找到一种更好的方法来测试我的addtoken函数的哪一部分出错了。 有任何想法吗? 任何帮助将不胜感激!

好吧,我绝对有什么错的想法:

anagramList* new_node = (anagramList*)malloc(sizeof(anagramList*));

在这里,您仅分配一个指针 ,而不分配节点本身。 做:

anagramList* new_node = malloc(sizeof(anagramList));


注意:不建议强制转换malloc的结果。

关于样式的注意事项:在例如end -> next ,空格不是常规的。 只需写完end->next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM