Я новичок в использовании Valgrind, и у меня проблема с попыткой интерпретировать результаты. У меня есть простая проблема «связанного списка», но Вальгринд говорит, что у меня есть некоторые проблемы в коде.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct str_list{
char *str;
struct str_list *next;
}str_list_t;
str_list_t*
split(char str[], char* sep){
str_list_t *first = NULL;
str_list_t *last = NULL;
char *token = strtok(str, sep);
while(token != NULL){
str_list_t *new_node = (str_list_t *)malloc(sizeof(str_list_t));
new_node->str = token;
new_node->next = NULL;
if (first == NULL){
first = new_node;
last = new_node;
}else{
last->next = new_node;
last = new_node;
}
token = strtok(NULL, sep);
free(new_node);
}
return first;
}
int main(){
char t[] = "Hello_World";
str_list_t * test = split(t, "_");
return 1;
}
и вывод Valgrind:
==9628== Invalid write of size 8
==9628== at 0x1087BF: split (test.c:26)
==9628== by 0x108828: main (test.c:40)
==9628== Address 0x51d7048 is 8 bytes inside a block of size 16 free'd
==9628== at 0x4C2CDDB: free (vg_replace_malloc.c:530)
==9628== by 0x1087EB: split (test.c:32)
==9628== by 0x108828: main (test.c:40)
==9628== Block was alloc'd at
==9628== at 0x4C2BBAF: malloc (vg_replace_malloc.c:299)
==9628== by 0x108782: split (test.c:18)
==9628== by 0x108828: main (test.c:40)
В частности, проблемы в этой строке:
last->next = new_node;
free(new_node);
str_list_t *new_node = (str_list_t *)malloc(sizeof(str_list_t));