我已尽一切努力,并且在StackOverflow上读取每个线程均无济于事。您能帮我理解我在代码中遇到的Valgrind错误吗?
void stringArrayAppend(char** array, int numLines, char* toAppend) {
printf("Size of toAppend: %zu\n", sizeof(toAppend));
printf("Size of character pointer: %zu\n", sizeof(char*));
array = realloc(array, sizeof(char*) * numLines);
if (array == NULL) {
printf("Could not reallocate string array to append string %s\n", toAppend);
freeStringArray(array, numLines);
return;
}
array[numLines - 1] = toAppend;
}
输出:
First element of string upon creation: a
Second element of string upon creation:
Size of string upon creation: 8
Enter the poem:
the
Size of input: 1
Size of character: 1
Size of toAppend: 8
Size of character pointer: 8
==24889== Invalid read of size 8
==24889== at 0x400A53: main (reversePoem.c:140)
==24889== Address 0x5205040 is 0 bytes inside a block of size 8 free'd
==24889== at 0x4C3016F: realloc (vg_replace_malloc.c:826)
==24889== by 0x400856: stringArrayAppend (reversePoem.c:62)
==24889== by 0x400A1B: main (reversePoem.c:137)
==24889== Block was alloc'd at
==24889== at 0x4C2DE56: malloc (vg_replace_malloc.c:299)
==24889== by 0x4006E1: createStringArray (reversePoem.c:10)
==24889== by 0x400949: main (reversePoem.c:95)
==24889==
Line string array after appending line string: [(null)]
First element of string upon creation: a
Second element of string upon creation:
Size of string upon creation: 8