Thread: How does free() know how much memory it has to free?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    120

    How does free() know how much memory it has to free?

    Hello

    My question is, if I char a* = malloc(10); and then I free(a); how will free know the size of the data pointed to by a?

    And is there any way to release a specific number of bytes in a given location?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can only free exactly what was malloced. The system may not actually keep track of the size directly (only indirectly by saying "the next available piece of memory is here"), or it might keep the size around somewhere.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by shiroaisu View Post
    My question is, if I char a* = malloc(10); and then I free(a); how will free know the size of the data pointed to by a?
    It doesn't really matter, in practice, unless you are implementing malloc() and free(). All you need to know is that .... it does.

    One way is that malloc() (and related functions) maintains some table that keeps track of what memory has been allocated, and the size of each chunk.

    Another way is that malloc() uses some function provided by the operating system, and the operating system keeps track of what memory it has allocated for your program (or process).

    Either way, something has to keep track. One reason that invalid operations on pointers often result - later - in a crash during a free() call is that the invalid pointer operation trashes a random area of memory. If that memory happens to correspond with some table that malloc()/free(), or the operating system, use to keep track of allocated memory, then hell breaks loose.

    Quote Originally Posted by shiroaisu View Post
    And is there any way to release a specific number of bytes in a given location?
    Generally, no.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    As everyone has told you, this is not defined by the standard and is left up to the individual implementation; hence there are multiple right answers. If you want to take a look at individual implementations to get an idea of how it works you could read through:
    That should get you started on providing some details behind what others have already answered. Beyond just academic curiousity though, there really is no reason into looking into how it works as grumpy has said.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    As everyone has told you, this is not defined by the standard and is left up to the individual implementation; hence there are multiple right answers. If you want to take a look at individual implementations to get an idea of how it works you could read through:
    That should get you started on providing some details behind what others have already answered. Beyond just academic curiousity though, there really is no reason into looking into how it works as grumpy has said.
    Actually it's left to the Operating System, Andrew.

    In windows the underlying HeapAlloc() and HeapFree() respectively build and remove structs forming a linked list that frames each allocation. To free memory, simply connect the struct before to the one after and set the pointer to NULL. Depending on the version of windows these structs keep a pointer to the previous block, a pointer to the next block and the size allocated. The returned pointer seen by application programs is actually to the end of the struct (the first available byte of the block) so when you free() memory it simply indexes -sizof(struct) and NULLS the pointers.

    This is one of the reasons why it's a real bad idea to distube the first pointer from malloc(), if you increment it away from the point where it knows how to find the struct, free() fails and your program leaks memory.
    Last edited by CommonTater; 09-08-2011 at 05:45 PM.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Actually it's left to the Operating System, Andrew.
    Hence why I said the individual implementation, the libraries use OS calls to handle it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    Actually it's left to the Operating System, Andrew.
    That is often true, but not always.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    That is often true, but not always.
    At some level it is always true... The OS has to martial out memory to applications and processes. If it were otherwise you'd end up with multiple processes accessing the same memory segments and all the fun that would cause. No matter that you've written your own memory manager (which is totally possible in C) at some underlying level you had to get that block of memory from an Operating System call... at which point the process becomes highly dependent on the OS.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're assuming all developed software is hosted on an operating system, Tater. Try writing or maintaining an application that runs "on the metal" (i.e. interfaces directly to hardware with minimal or no intervening operating system) and you will find the application, or library it uses, has to deal with aspects of the "fun" that you describe.

    Modern operating systems came to exist because of that recurring "fun" people had developing large software systems that had to interface directly to hardware. All that happens now, mostly, is that the fun is left for those who write the operating systems. But there are still examples of modern software being developed without benefit of being hosted on an operating system, even though a lot of people try to pretend otherwise.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    You're assuming all developed software is hosted on an operating system, Tater.
    For the purposes of this discussion I'm betting that's a totally safe assumption...

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CommonTater View Post
    For the purposes of this discussion I'm betting that's a totally safe assumption...
    .... and I am not.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by grumpy View Post
    You're assuming all developed software is hosted on an operating system, Tater.
    For the purposes of this thread yes it'd be a true statement.
    Quote Originally Posted by grumpy View Post
    Try writing or maintaining an application that runs "on the metal" (i.e. interfaces directly to hardware with minimal or no intervening operating system) and you will find the application, or library it uses, has to deal with aspects of the "fun" that you describe.
    Most of those "baremetal" types aren't multitasking systems so having a memory manager is irrelevant since there's no translation involved; it's all physical.
    Quote Originally Posted by grumpy View Post
    Modern operating systems came to exist because of that recurring "fun" people had developing large software systems that had to interface directly to hardware. All that happens now, mostly, is that the fun is left for those who write the operating systems. But there are still examples of modern software being developed without benefit of being hosted on an operating system, even though a lot of people try to pretend otherwise.
    Are you implying MS-DOS

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itCbitC View Post
    Are you implying MS-DOS
    More likely it's a reference to microcontrollers (PIC, etc.)

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by CommonTater View Post
    More likely it's a reference to microcontrollers (PIC, etc.)
    I was being facetious when I said that; microcontrollers btw don't multitask so walking over another process' memory doesn't factor in.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itCbitC View Post
    microcontrollers btw don't multitask so walking over another process' memory doesn't factor in.
    Can't remember the part number off the top of my head, but there are a couple of new ones that do have some rudimentary "task switching" ability that could lead to interrupt driven multitasking.

    (I'll see if I can find it and will update here if I do...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new license-free lock-free data structure library published
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 12-22-2009, 02:33 AM
  2. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  3. What's cooler than free boobs? 5 free assembly books from AMD.
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-13-2003, 08:22 PM
  4. free memory
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2002, 05:39 AM