Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > doubt about freeing memory

Reply
Thread Tools

doubt about freeing memory

 
 
orium69@gmail.com
Guest
Posts: n/a
 
      02-23-2006
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...

 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-23-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-23-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-23-2006
(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
memory allocation and freeing memory Rodrigo Dominguez C Programming 11 06-14-2005 11:54 PM
freeing memory Harald Kirsch Java 0 04-22-2005 09:17 AM
freeing memory Rajesh.Rapaka Java 17 04-21-2005 10:11 PM



Advertisments