mallo/free issue

Can some explain me how this code disturbs the memory partition blocks

char* p = (char*) malloc(100);

memcpy(p,somedata, 200);

free(p) // I am getting error here that next block of memory was over written.
1
2
3
4
5
char* p = (char*) malloc(100);

memcpy(p,somedata, 200);

free(p);


You're really having trouble figuring out what's going on there?
Its true, but when ever the allocated memory is getting freed that time we are getting error, it say that next block of memory is corrupted. can you suggest some good links to understand the memory partitioning.
closed account (S6k9GNh0)
It's implementation defined, based on OS. There's tons of different malloc implementations. Although, it shouldn't really matter to your application.
it say that next block of memory is corrupted.
well, with this 200 you overwrite the next block of memory (especially the header). No wonder that it gets corrupted
when ever the allocated memory is getting freed that time we are getting error, it say that next block of memory is corrupted
That's because the heap's been corrupted earlier, either with a buffer overrun or reusing a free'd block. The code you posted is fine, apart from the missing check for NULL.

EDIT: Didn't notice the 100 byte allocation, and 200 byte write. Well, there's your buffer overwrite.
Last edited on
well, with this 200 you overwrite the next block of memory (especially the header). No wonder that it gets corrupted


Yeah, we are checking the header of the next block while freeing. I really didn't understand the concept of header.
Topic archived. No new replies allowed.