Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > where do the extra bytes go while using Malloc ?

Reply
Thread Tools

where do the extra bytes go while using Malloc ?

 
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      10-23-2007
Richard Heathfield wrote:

> [This reply written in comp.lang.c, and followups set to that group.]
>
> karthikbalaguru said:


>>In the case of heap , to keep track of a single chunk of memory it
>>requires 8 bytes of information.


> Not necessarily. It might need only 1, or it may need a dozen or a
> thousand.


>>That is, it requires 4 bytes to hold the size, and 4 bytes to hold the
>>pointer to the next block of memory.


The way malloc() works, keeping track of the length of the allocated
block, requires at least that the length be stored somewhere.

The allocator used by OS/360, called GETMAIN/FREEMAIN, works a little
differently. There is no overhead in the allocated memory.
The length and starting address are specified for FREEMAIN.
One can free part of an allocated region, even as a hole in the
middle of a previously allocated region. The result is that the
free memory contains a linked list, but the allocated memory has
no overhead. (I believe allocations must be in multiples of
eight bytes, though, if no other reason than to guarantee alignment.)

I do wonder, though, why C supplies no routine to determine the length
of an allocated block.

-- glen

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-23-2007
Filip Larsen wrote:
> karthikbalaguru wrote:
>
>> How does malloc/calloc then allocate the exact size of data as
>> there must be some wastage of memory that has to be taken into
>> account while allocating in heap memory (Memory consumed for
>> managing the information in terms of linked list) ?

>
> While this in principle is an implementation issue it is to my
> knowledge fairly common to place (some of) this management data
> for each malloc block just in front of the block. Or it was, at
> least, on the systems I was using 10-20 years ago.


You can examine the C code for a fairly portable malloc in
nmalloc.zip, written for DJGPP. A malloc package cannot be
portable, since it depends on system knowledge. However nmalloc is
pretty close to standard C, requires the system provide an sbrk()
function to supply memory, and compiles under gcc. The gcc
requirement is due to a set of development macros, none of which
are in operation for the final package. You can find nmalloc.zip
at:

<http://cbfalconer.home.att.net/download/>

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      10-23-2007

"karthikbalaguru" <(E-Mail Removed)> wrote in message
> That is, Malloc should consume more space to allocate the desired
> amount of data . where do the extra bytes go ?
>

The trick is to store the control block just before the pointer you return.
Then free() subtracts the block from the pointer passed to it, and has all
the information it needs to make the memory avaialble for further
allocations.

That's not the only way of doing it, and in fact most modern systems use a
rather different method for small allocations, but it is how a simple
malloc() works. If you check my website you will find a memory allocation
package. It is one of the free sample chapters of Basic Algorithms.


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      10-23-2007

"glen herrmannsfeldt" <(E-Mail Removed)> wrote in message
> I do wonder, though, why C supplies no routine to determine the length
> of an allocated block.
>

Should you return the length allocated or the length asked for? If the
second you need an extra field, if the first you need to educate programmers
not to use the function as a lazy array length.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      10-24-2007
karthikbalaguru wrote:
> Hi,
> In the case of heap , to keep track of a single chunk of memory it
> requires 8 bytes of information.
> That is, it requires 4 bytes to hold the size, and 4 bytes to hold the
> pointer to the next block of memory. So, For every additional chunk,
> even if it is only one byte long, these 8 bytes are required again, in
> addition to the 1 byte actually needed to store the chunk itself.
>
> So, there should be wastage of memory for managing the linked list
> (heap).
> But, How does malloc/calloc then allocate the exact size of data as
> there must be some wastage of memory that has to be taken into account
> while allocating in heap memory (Memory consumed for managing the
> information in terms of linked list) ?
>
> That is, Malloc should consume more space to allocate the desired
> amount of data . where do the extra bytes go ?


British readers of the newsgroup might identify with me on this thread.

I can imagine Mark Benton in his bank manager persona explaining that
the extra bytes are saved up for the Directors' christmas party... Quite
what the Directors do with the bytes, I haven't worked out.
 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      10-24-2007
On Oct 24, 3:50 am, "Malcolm McLean" <(E-Mail Removed)> wrote:
> "karthikbalaguru" <(E-Mail Removed)> wrote in message
> > That is, Malloc should consume more space to allocate the desired
> > amount of data . where do the extra bytes go ?

>
> The trick is to store the control block just before the pointer you return.
> Then free() subtracts the block from the pointer passed to it, and has all
> the information it needs to make the memory avaialble for further
> allocations.
>
> That's not the only way of doing it, and in fact most modern systems use a
> rather different method for small allocations, but it is how a simple
> malloc() works. If you check my website you will find a memory allocation
> package. It is one of the free sample chapters of Basic Algorithms.
>
> --
> Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm


Thx to all of you for the info

Karthik Balaguru

 
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
Does return-by-value mean extra copies and extra overhead? mathieu C++ 3 09-04-2009 04:25 PM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
copy XML file -- three extra bytes??? martin ASP .Net 4 03-06-2004 12:58 AM



Advertisments