473,406 Members | 2,713 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,406 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 1246
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;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.