简体   繁体   中英

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

I'm a newbie in c, when I'm writing a toy program of adding node to a linked list, which is as follows:

#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;
}

When I read from stdin, it works just fine and gives right output, when I'm testing with valgrind, message appears:

$ 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)

I really have no idea why the valgrind test fail and I haven't figure out a better way to test which part of my addtoken function has went wrong. Any ideas? Any help would be greatly appreciated!

Well, I definitely have an idea of what is wrong:

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

here you allocate only a pointer but not the node itself. Do:

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


Note: casting the result of malloc is not advised.

Note on style: in eg end -> next the spaces are not customary. Just write end->next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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