C free(): invalid pointer

C free(): invalid pointer

Youre attempting to free something that isnt a pointer to a freeable memory address. Just because something is an address doesnt mean that you need to or should free it.

There are two main types of memory you seem to be confusing – stack memory and heap memory.

  • Stack memory lives in the live span of the function. Its temporary space for things that shouldnt grow too big. When you call the function main, it sets aside some memory for your variables youve declared (p,token, and so on).

  • Heap memory lives from when you malloc it to when you free it. You can use much more heap memory than you can stack memory. You also need to keep track of it – its not easy like stack memory!

You have a few errors:

  • Youre trying to free memory thats not heap memory. Dont do that.

  • Youre trying to free the inside of a block of memory. When you have in fact allocated a block of memory, you can only free it from the pointer returned by malloc. That is to say, only from the beginning of the block. You cant free a portion of the block from the inside.

For your bit of code here, you probably want to find a way to copy relevant portion of memory to somewhere else…say another block of memory youve set aside. Or you can modify the original string if you want (hint: char value 0 is the null terminator and tells functions like printf to stop reading the string).

EDIT: The malloc function does allocate heap memory*.

9.9.1 The malloc and free Functions

The C standard library provides an explicit allocator known as the malloc package. Programs allocate blocks from the heap by calling the malloc function.

~Computer Systems : A Programmers Perspective, 2nd Edition, Bryant & OHallaron, 2011

EDIT 2: * The C standard does not, in fact, specify anything about the heap or the stack. However, for anyone learning on a relevant desktop/laptop machine, the distinction is probably unnecessary and confusing if anything, especially if youre learning about how your program is stored and executed. When you find yourself working on something like an AVR microcontroller as H2CO3 has, it is definitely worthwhile to note all the differences, which from my own experience with embedded systems, extend well past memory allocation.

From where did you get the idea that you need to free(token) and free(tk)? You dont. strsep() doesnt allocate memory, it only returns pointers inside the original string. Of course, those are not pointers allocated by malloc() (or similar), so free()ing them is undefined behavior. You only need to free(s) when you are done with the entire string.

Also note that you dont need dynamic memory allocation at all in your example. You can avoid strdup() and free() altogether by simply writing char *s = p;.

C free(): invalid pointer

You cant call free on the pointers returned from strsep. Those are not individually allocated strings, but just pointers into the string s that youve already allocated. When youre done with s altogether, you should free it, but you do not have to do that with the return values of strsep.

Leave a Reply

Your email address will not be published.