473,320 Members | 1,856 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,320 software developers and data experts.

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 2652
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.googlegro ups.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.googlegro ups.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.ukwrites:
Malcolm McLean wrote, On 09/09/07 18:02:
>"siddhu" <si***********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegr oups.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_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."
-- 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.ukwrote:
>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
--
"Consideration 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.ukwrote:
>>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
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...
46
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. ...
30
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 =...
72
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
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...
10
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:...
4
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...
11
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...
94
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...
13
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....
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
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.