473,698 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how much memory to free?

Dear experts,

If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be freed
and make it available for further allocations?

Regards,
Siddharth

Sep 9 '07 #1
7 2676
siddhu wrote:
Dear experts,

If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be freed
and make it available for further allocations?
However much you asked for will be freed. The necessary information for this
is recorded when you call malloc.
Sep 9 '07 #2

"siddhu" <si***********@ gmail.comwrote in message
news:11******** **************@ 57g2000hsv.goog legroups.com...
Dear experts,

If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be
freed and make it available for further allocations?
The secret is that a control block exists _before_ the pointer returned by
malloc(). free() simply subtracts a few bytes to access it.

Not all mallocs work exactly like this, but a vanilla memory allocation
system does.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Sep 9 '07 #3
siddhu wrote:
Dear experts,

If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be freed
and make it available for further allocations?
Always check the FAQs.

<http://c-faq.com/malloc/freesize.html>

Brian
Sep 9 '07 #4
Malcolm McLean wrote, On 09/09/07 18:02:
>
"siddhu" <si***********@ gmail.comwrote in message
news:11******** **************@ 57g2000hsv.goog legroups.com...
>Dear experts,

If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be
freed and make it available for further allocations?
The secret is that a control block exists _before_ the pointer returned
by malloc().
Or after, or somewhere else entirely.
free() simply subtracts a few bytes to access it.
Only on some systems.
Not all mallocs work exactly like this, but a vanilla memory allocation
system does.
What you mean is that is how it works on some systems, whilst others
work differently. I don't see food flavourings have to do with it. At
least, I can't find references to a memory allocation system called
"vanilla".

To the OP, you don't need to know how it knows, all you need to know is
it gets handled for you.
--
Flash Gordon
Sep 9 '07 #5
Flash Gordon <sp**@flash-gordon.me.ukwri tes:
Malcolm McLean wrote, On 09/09/07 18:02:
>"siddhu" <si***********@ gmail.comwrote in message
news:11******* *************** @57g2000hsv.goo glegroups.com.. .
>>If I do free(p); memory pointed by p is freed and is available for
further allocations in the process.
But how does it decide about how much memory (size) has to to be
freed and make it available for further allocations?
The secret is that a control block exists _before_ the pointer
returned by malloc().

Or after, or somewhere else entirely.
>free() simply subtracts a few bytes to access it.

Only on some systems.
>Not all mallocs work exactly like this, but a vanilla memory
allocation system does.

What you mean is that is how it works on some systems, whilst others
work differently. I don't see food flavourings have to do with it. At
least, I can't find references to a memory allocation system called
"vanilla".
"Vanilla" is obviously a colloquial term for "ordinary".
To the OP, you don't need to know how it knows, all you need to know
is it gets handled for you.
Agreed.

It can be useful, though, to see an example of *how* the size can be
stored. If you're not familiar with memory allocation, being told
that free "just knows" how many bytes to release can be confusing.
That's why the question "How does free know how many bytes to free?"
is a FAQ (7.26, to be precise). Understanding that, *for example*,
the size can be stored just before the allocated chunk, perhaps along
with some other metadata, tells you that there's nothing magical about
it.

But it's also very important to know that that's just one possible
strategy. For example, an implementation that stores all its metadata
(i.e., everything other than the actual allocated chunks of memory) in
a separate location can be more robust in the presence of code that
writes past the end of an allocated object.

(I think K&R has a sample memory allocator; reading that code can be
instructive.)

--
Keith Thompson (The_Other_Keit h) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 9 '07 #6
In article <if************ @news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwro te:
>The secret is that a control block exists _before_ the pointer returned
by malloc().
>Or after
That would be a neat trick. How does it work?

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 9 '07 #7
Richard Tobin wrote, On 10/09/07 00:40:
In article <if************ @news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwro te:
>>The secret is that a control block exists _before_ the pointer returned
by malloc().
>Or after

That would be a neat trick. How does it work?
A fat pointer with half of it pointing at the data and half pointing a
the control block ;-) Or maybe just the length.

Alternatively, and slightly more seriously, there could be a region used
to store all the control blocks which is after all of the malloced regions.

However, it is all done by magic so as the programmer you don't need to
know. :-)

OK, so I posted without my brain engaged. However some real
implementations don't store a control block before malloced region.
--
Flash Gordon
Sep 10 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2653
by: aaaaa | last post by:
Hi all, Does anybody know if STLPort or SGI STL standard allocators do memory pooling for the list, map and set? Also I have had a look at the BOOST pool_alloc (to be used as a pooling allocator for lists), but looking into the code it doesn't seem to ever release (to the global ::free) the memory that was once allocated. I can understand that such memory can be re-used if I have another list
46
4140
by: sbayeta | last post by:
Hi, I'd like to know who is responsible of memory recycling and defragmentation in a C/C++ program, assuming all the memory allocation/deallocation is done using malloc/free or new/delete. Thanks
30
3728
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
72
3597
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
5
307
by: RoSsIaCrIiLoIA | last post by:
why not to build a malloc_m() and a free_m() that *check* (if memory_debug=1) if 1) there are some errors in bounds of *all* allocated arrays from them (and trace-print the path of code that make the error and exit) just when malloc_m and free_m start. (I use a 1100 static array for write the path (like a queue) of called function, operator, etc and write using '+' where is find the memory error) 2) if the pointer to free is alredy...
10
2772
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions: "At a certain point in the code you may be unsure if a particular block is no longer needed. If you free() this piece of memory, but continue to access it (probably via a second pointer to the same
4
6010
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over data from August or any prior month for that matter works fine which is why this is so weird. Note that June 2004 through today is stored in the same f_pageviews table. Nothing has changed on the server in the last couple of months. I upgraded...
11
3947
by: seberino | last post by:
Suppose a C extension locally built an array of PyObject* 's as follows... my_array = malloc(n * sizeof(PyObject*)); for (i = 0; i < n; i++) { my_array = PyList_New(0); } Q1: Must I do a Py_DECREF(my_array) on all elements before exiting this C extension function? (What if
94
4725
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
13
2850
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure. It's most possibly caused by trac and it's dependencies, but several components of the OS where updated, too.
0
8600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9155
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7711
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5859
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2322
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.