c - Valgrind Reports Invalid Realloc -


i'm trying backfill knowledge of c memory management. i've come scripting , managed background, , want learn more c , c++. end i've been reading few books, including 1 included example of using realloc trim string of whitespace:

#include <stdio.h> #include <stdlib.h> #include <string.h>  char* trim(char* phrase) {   char* old = phrase;   char* new = phrase;    while(*old == ' ') {     old++;   }    while(*old) {     *(new++) = *(old++);   }    *new = 0;    return (char*)realloc(phrase, strlen(phrase)+1); }  int main () {   char* buffer = (char*)malloc(strlen("  cat")+1);   strcpy(buffer, "  cat");    printf("%s\n", trim(buffer));    free(buffer);   buffer=null;    return 0; } 

i dutifully copied example, , compiled c99 -wall -wpointer-arith -o3 -pedantic -march=native. don't compile errors, , app runs , what's promised in book, when run against valgrind error invalid realloc.

==21601== memcheck, memory error detector ==21601== copyright (c) 2002-2013, , gnu gpl'd, julian seward et al. ==21601== using valgrind-3.10.0.svn , libvex; rerun -h copyright info ==21601== command: ./trim ==21601==  ==21601== invalid free() / delete / delete[] / realloc() ==21601==    @ 0x402b3d8: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==21601==    0x804844e: main (in /home/mo/programming/learning_pointers/trim) ==21601==  address 0x4202028 0 bytes inside block of size 6 free'd ==21601==    @ 0x402c324: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==21601==    0x80485a9: trim (in /home/mo/programming/learning_pointers/trim) ==21601==    0x804842e: main (in /home/mo/programming/learning_pointers/trim) ==21601==  ==21601==  ==21601== heap summary: ==21601==     in use @ exit: 4 bytes in 1 blocks ==21601==   total heap usage: 2 allocs, 2 frees, 10 bytes allocated ==21601==  ==21601== 4 bytes in 1 blocks lost in loss record 1 of 1 ==21601==    @ 0x402c324: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==21601==    0x80485a9: trim (in /home/mo/programming/learning_pointers/trim) ==21601==    0x804842e: main (in /home/mo/programming/learning_pointers/trim) ==21601==  ==21601== leak summary: ==21601==    lost: 4 bytes in 1 blocks ==21601==    indirectly lost: 0 bytes in 0 blocks ==21601==      possibly lost: 0 bytes in 0 blocks ==21601==    still reachable: 0 bytes in 0 blocks ==21601==         suppressed: 0 bytes in 0 blocks ==21601==  ==21601== counts of detected , suppressed errors, rerun with: -v ==21601== error summary: 2 errors 2 contexts (suppressed: 0 0) 

so please me understand why it's consider invalid realloc. example crap? there i'm missing? know according specs, realloc expects pointer have been created malloc, because realloc in function? or valgrind confused because they're in separate functions? i'm not complete idiot (most days), right kind of feel 1 not seeing issue.

thanks in advance!

you're trying free original pointer, not reallocd one. can fix by:

buffer = trim(buffer) 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -