8

This question was asked to me in an interview.

Suppose char *p=malloc(n) assigns more than n,say N bytes of memory are allocated and free(p) is used to free the memory allocated to p.

can heap manager perform such faulty allocation ? what happens now, will n bytes are freed or N bytes are freed?

is there any method to find how much memory is freed?

EDIT

is there any method to find how much memory is freed?

better than nothing,

mallinfo() can shed some light as pointed by "Fred Larson"

1
  • 6
    Why do you think such an allocation is faulty? Regardless of how many "actual" bytes malloc() allocated, you can use only n bytes, and free() will free all of the allocated bytes. Most malloc implementations allocate more space then requested for efficiency reasons. An implementation defines malloc and free, and by definition, they have to agree on a scheme of allocation. Feb 25, 2010 at 17:59

5 Answers 5

9

Yes, that's what happens almost every time do you a malloc(). The malloc block header contains information about the the size of the block, and when free() is called, it returns that amount back to the heap. It's not faulty, it's expected operation.

A simple implementation might, for instance, store just the size of the block in the space immediately preceding the returned pointer. Then, free() would look something like this:

void free(void *ptr)
{
    size_t *size = (size_t *)ptr - 1;

    return_to_heap(ptr, *size);
}

Where return_to_heap() is used here to mean a function that does the actual work of returning the specified block of memory to the heap for future use.

5
  • Is there a method to find out how much memory is freed?
    – Rozuur
    Feb 25, 2010 at 18:07
  • @rozuur: All memory allocated by malloc() will be freed by free(). I think your real question is how to find out how much memory was allocated. (And the answer? At least as much as was requested, if the allocation was successful.)
    – Bill
    Feb 25, 2010 at 18:33
  • 1
    rozuur: look at this page: msdn.microsoft.com/en-us/library/ms220938(VS.80).aspx it gives an idea about what is actually done when you ask for a chunk of memory. Most implementations are very similar to this.
    – ThePosey
    Feb 25, 2010 at 18:40
  • 1
    @rozuur The important point is it doesn't matter how big the allocation really is. These are implementation details and you shouldn't worry about them too much, unless you suspect they are causing you problems, which most of the time they will not.
    – asveikau
    Feb 25, 2010 at 19:15
  • @Rozur, and for posterity! 1) All memory allocate will be released (N, as per the question). 2) Whether or not you can find out exactly how much memory is allocated depends on whether the memory manager publishes this information. 3) There is no standard function to obtain the number of bytes allocated by malloc() (although different vendors may choose to provide such a function). 4) Many memory managers round up n to the next power of two, thus ensuring you never waste more than half your memory. Jul 9, 2013 at 17:26
4

Yes, the heap manager is allowed to return a block of more than n bytes. It is completely safe (and required!) to free the returned pointer using free, and free will deallocate all of it.

Many heap implementations track their allocations by inserting blocks of metadata into the heap. free will look for that metadata to determine how much memory to deallocate. This is implementation-specific, though, so there's no way to know how much malloc gave you, and generally, you shouldn't care.

1

Generally the heap manager will free whatever it allocated. It stores this info somewhere, and looks it up when free() is called.

A heap manager is not "faulty" if it allocates more memory than was requested. Heap managers often work with fixed block sizes, and will round up to the next appropriate block size when satisfying a request. The heap manager's job is to be as efficient as possible, and often big efficiencies result from a few small inefficiencies.

1
  • "often big efficiencies result from a few small inefficiencies" Nice quote 8^)
    – caseman
    Feb 25, 2010 at 19:10
1

This is the default behavior of malloc. It will return NULL or a pointer to a section of memory at least as long as the one you asked for. So yes free must be able to handle getting rid of memory longer than what was asked for.

Finding out how much memory was actually free or allocated is a platform specific question.

1

Other answers have explained well how the block size is handled. To find out how much memory is freed, the only solution I can think of is to call mallinfo() before and after the free.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.