Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Silverbolter

how malloc works...

This topic is 5330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Just reading about memory allocators right now here: http://gee.cs.oswego.edu/dl/html/malloc.html . I understand that when you request say sizeof(char) with malloc(), on some machines the actual bytes returned isn''t the exact sizeof(char), but instead aligned to the machine''s word size. My question is, since malloc() will need extra bytes to do housekeeping, some information relating to the size of the block, whether its in use or not, etc. will need to be placed near the returned block of memory. However I have a question as to where this information is placed in relation to this returned block? Before? After? If its before (which means right at the pointer) then when you dereference the pointer you will get that housekeeping information too, which isn''t really what you want. If it''s after, then you don''t really know how far you must look? If anyone can point me to some articles or answer these questions it would be great. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
It indeed cannot be stored after the block. It can, on the other hand, be stored at the beginning of the block. malloc() would simply not return a pointer to the very beginning of the ''raw'' memory block, but a pointer offset by the size of the housekeeping information block. That way, you don''t *get* the housekeeping data, but the library can find it easily.



“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
The housekeeping information is generally stored _before_ the block. Otherwise, free() would have no idea where to look for it, since it doesn't know the size of the block until after it already has the housekeeping information.

Let's say that malloc() has been asked to allocate 16 bytes of memory. Let's also say it uses 4 bytes for housekeeping info, placed immediately before the block. So that's a 20-byte block in all, with 16 bytes meant for use by the application. malloc() decides to put those 20 bytes at the location 100. So the block includes bytes 100 to 119.

Now, does malloc return "100"? No. It returns "104". It doesn't tell the program about the space for the housekeeping information; as the program never accesses bytes 100-103. But when "104" is later passed into free(), it knows to subtract 4 to find the actual location of the housekeeping information.

EDIT: Noooo! one second!


"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on January 12, 2004 2:04:52 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Sneftel
EDIT: Noooo! one second!


BOOYA !


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!