Re: How does free() know how many bytes to free???
- From: Josef Moellers <josef.moellers@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 27 Nov 2007 10:11:03 +0100
MAx wrote:
Hi,
A call to free() is used to de allocate memory which has been
allocated by a malloc()
or a calloc() call.
How does free() know how many bytes to deallocate?
The size is stored in bytes just before the useful area. Note that additional bits are used for other purposes.
Here's a small (32 bit!) program to show:
#include <stdlib.h>
#include <stdio.h>
void
all(size_t s)
{
unsigned char *ucp;
int i;
ucp = malloc(s);
printf("%#x:", s);
for (i = 0; i < 8; i++)
printf(" %02x", ucp[i-8]);
printf("\n");
}
int
main(int argc, char *argv)
{
all(0x1234);
all(0x12340);
all(0x123400);
}
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
.
- Follow-Ups:
- Re: How does free() know how many bytes to free???
- From: Tim Roberts
- Re: How does free() know how many bytes to free???
- References:
- Prev by Date: Re: How does free() know how many bytes to free???
- Next by Date: Re: new filesystem module
- Previous by thread: Re: How does free() know how many bytes to free???
- Next by thread: Re: How does free() know how many bytes to free???
- Index(es):
Relevant Pages
|