Sign in to follow this  

how to free malloc'd data

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm guessing malloc allocates stuff off the heap which would mean you need to free it up when you're done, correct? right now i'm doing something like (btw, Pattern is a struct) Pattern *head = (Pattern*)malloc(sizeof(Pattern*)); and in the destructor i'm doing free(head); which of course, causes the program to crash. what am i doing wrong here? thanks in advance...

Share this post


Link to post
Share on other sites
To elaborate a bit you're probably overwriting the memory you allocated and corrupting the heap since you only allocated room for a pointer instead of room for the whole object. Most heap implementations will die in strange and mysterious ways when you do this.

Since it sounds like you're using C++ you should probably use "head = new Pattern" and "delete head" instead of malloc/free but that's a different issue.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anon Mike
To elaborate a bit you're probably overwriting the memory you allocated and corrupting the heap since you only allocated room for a pointer instead of room for the whole object. Most heap implementations will die in strange and mysterious ways when you do this.

Since it sounds like you're using C++ you should probably use "head = new Pattern" and "delete head" instead of malloc/free but that's a different issue.


Just a note, don't mix new/del and malloc/free for the same memory areas. Like, ever. Choose one and stick to it :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by h2mprod
Pattern *head = (Pattern*)malloc(sizeof(Pattern*));
free(head);


In C, prefer:

Pattern *head = malloc(sizeof *head);
free(head);

If the compiler complains, it thinks you're compiling C++. If you're using C++, avoid malloc/free and use new/delete (which is what it sounds like you are doing).

Share this post


Link to post
Share on other sites
The problem is that your code:

Pattern *head = (Pattern*)malloc(sizeof(Pattern*));
...
free(head);

is only allocating room for a pointer and not for your structure. You want "sizeof(Pattern)" inside malloc, not "sizeof(Pattern*)".

Malloc does not return raw memory, it returns a memory region inside of a data structure. I don't know how the MS version of malloc works specifically, but the version in the GNU C library stores, at the beginning of each chunk, the size of the previous chunk, the size of the current chunk, a forward pointer to the next free chunk (if the current chunk is unallocated) and a back pointer to the previous free chunk (if the current chunk is unallocated).

At first, the heap is just one large area of free memory. As the program executes, the heap is broken into smaller chunks as individual pieces are allocated to service the requests of the program. The free chunks are sorted by size in various bins each containing a doubly linked list of free chunks. If the current chunk is allocated (it is not in one of the free lists), the area for the two pointers is used as data.

Because you are allocating only a pointer size region and writing more data to it, you are corrupting the boundary tags (the four values) at the beginning of one or more adjacent chunks of memory. All goes well for a while, then, you try to free the memory. When you do this, the program attempts to use the information in the corrupted boundary tags and crashes.

I hope this helps.

Steven
alexander dot s _at_ mccd dot edu

Share this post


Link to post
Share on other sites

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this