473,287 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 1242
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
11
by: Rodrigo Dominguez | last post by:
there are sometimes that I use third party libraries, I use some functions that returns char * or structs, etc. sometimes the memory that is returned by those libraries, when I try to free this...
6
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT...
4
by: Atul Sureka | last post by:
Hi, I want to free the object memory in C# - like we do using 'delete' keyword in C++. Lets say I have an object of some class and I want to explicitly free the memory. C# do not have any free...
5
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
9
by: david | last post by:
I will past only two segments from the code it should be enough to see what I did wrong, I think I know there I made a mistake, but how to fix it I can not tell. This why I need help from you all....
11
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it...
25
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.