470,815 Members | 1,147 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

doubt about freeing memory

Hi everyone!

I have a doubt, when I free an alloced memory, how does the compiler
know how many bytes I have alloced forward the pointer.

eg:

int *coiso;

coiso=(int *)malloc(sizeof(int)*10);

free(coiso);

How the compiler know that it has to free from coiso to
coiso+(sizeof(int)*10)?

thanks

sorry for my probably realy bad english...

Feb 23 '06 #1
3 1137
or*****@gmail.com wrote:
eg:

int *coiso;

coiso=(int *)malloc(sizeof(int)*10);
Casting the result of 'malloc' is not a good idea. Why don't you just do

coiso = malloc(sizeof(int)*10);

An even better idea would be doing

coiso = malloc(10 * sizeof *coiso);
free(coiso);

How the compiler know that it has to free from coiso to
coiso+(sizeof(int)*10)?


Short answer: somehow.

This is an implementation detail, which you are not supposed to worry about. For
example, the run-time library might store the size originally passed to 'malloc'
somewhere in a secret place. 'free' might know how to retrieve that size when
necessary.

--
Best regards,
Andrey Tarasevich
Feb 23 '06 #2
or*****@gmail.com wrote:
Hi everyone!

I have a doubt, when I free an alloced memory, how does the compiler
know how many bytes I have alloced forward the pointer.

eg:

int *coiso;

coiso=(int *)malloc(sizeof(int)*10);

free(coiso);

How the compiler know that it has to free from coiso to
coiso+(sizeof(int)*10)?

It isn't the compilers problem, it is the underlying implementation of
the memory allocator that keeps track of allocated block sizes.

--
Ian Collins.
Feb 23 '06 #3
or*****@gmail.com writes:
I have a doubt, when I free an alloced memory, how does the compiler
know how many bytes I have alloced forward the pointer.

eg:

int *coiso;

coiso=(int *)malloc(sizeof(int)*10);

free(coiso);

How the compiler know that it has to free from coiso to
coiso+(sizeof(int)*10)?


This is question 7.26 in the comp.lang.c FAQ,
<http://www.c-faq.com/malloc/freesize.html>.

See also questions 7.7 and 7.7b. While you're at it, you might read
all of section 7, and the rest of the FAQ as well. It's a very good
resource.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by disco | last post: by
11 posts views Thread by Rodrigo Dominguez | last post: by
6 posts views Thread by Fernando Cacciola | last post: by
4 posts views Thread by Atul Sureka | last post: by
5 posts views Thread by Amogh | last post: by
66 posts views Thread by karthikbalaguru | last post: by
9 posts views Thread by david | last post: by
11 posts views Thread by vivek | last post: by
25 posts views Thread by Andreas Eibach | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.