malloc - C- How to free the following malloced memory -
can please me on how free 2 dimensional array here. tried using loop free error saying:
*** glibc detected *** ./assignment4: free(): invalid pointer: 0x0932b196 ***
the thing when free 0th element alone dont problem. when free elements [1] [7] error.
char ** stringone; stringone = malloc(sizeof(char*)*8); stringone[0] = malloc(sizeof(char)*6); stringone[0] = strtok(listofdetails[0], " "); for(i=1;i<8;i++) { stringone[i] = malloc(sizeof(char)*6); stringone[i] = strtok(null, " "); } for(i=0;i<8;i++) // freeing memory { free(stringone[i]); }
the strtok
function returns pointer string you're tokenizing, , pointer overwrites pointer allocate, leading lose pointers allocated memory.
there 2 solutions: don't allocate memory (and don't free), or copy allocated memory.
Comments
Post a Comment