4

If malloc calls VirtualAlloc() function to allocate memory (which allocates minimum 4Kb), how malloc allocates 4 bytes for int?

user3245337
  • 147
  • 9
  • How are you determining that malloc allocated 2 bytes for an int? If you asked for sizeof(int) it could have allocated 4kb and you would be none the wiser. – Vicky Jan 28 '14 at 16:16
  • std::cout << sizeof(a); // or sizeof(int), it prints 4 (sizeof returns in bytes) – user3245337 Jan 28 '14 at 16:19
  • fix it... int has usually 4 bytes instead of 2, but the question is good. – Wagner Patriota Jan 28 '14 at 16:20
  • Don't you mean it calls HeapAlloc()? – Alex K. Jan 28 '14 at 16:21
  • 2
    This might be interesting: http://en.wikipedia.org/wiki/C_dynamic_memory_allocation#Implementations – Bart Friederichs Jan 28 '14 at 16:23
  • 1
    If you've got K&R available, then it shows one way to implement `malloc()` et al. Fundamentally, `malloc()` keeps somewhere the information about the size of the block it has allocated and given to the programmer; often, this is just before the pointer you are given by `malloc()`. `malloc()` always rounds the allocation up to a convenient size (for it to use); often this is a multiple of 8 bytes for 32-bit systems and a multiple of 16 bytes for 64-bit systems. – Jonathan Leffler Jan 28 '14 at 16:26
  • @JonathanLeffler part of that is the requirement the returned address must be aligned to support the widest native type for the implementation and the hardware its performing upon, as I recall, which *can* chew up extra space even in sub allocations. But it certainly beats page-at-a-time. Nice comment. – WhozCraig Jan 28 '14 at 16:29
  • A good way to learn how malloc might do it is to write your own implementation. Given a method `VirtualAlloc`, how would *you* write `malloc` and `free`? (Try just `malloc` and `free`, and don't worry about `realloc`, etc, for now.) – Eric Lippert Jan 28 '14 at 19:14

2 Answers2

8

malloc requests memory from the OS in multiples of the page size (obviously, since the page size is by definition the quantum of allocated memory) and hands it out to you in smaller chunks.

That's not different than what all memory allocators do -- in fact, specialized memory allocators (e.g. Boost.Pool) that use malloc behind the scenes do exactly this once more: they allocate a bigger chunk of memory through malloc and hand it out to you in smaller pieces.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • +1 Not uncommonly also called a sub-allocator. Many (dare I say most) runtime libraries do something along these lines for you (or at least they used to when memory was *precious*). – WhozCraig Jan 28 '14 at 16:24
1

Application I am working on is internally using malloc.c implementation from Doug Lea (ftp://g.oswego.edu/pub/misc/malloc.c), it is widely used on many platforms.

This implementation is taking memory from system, in case of Windows in 64KB chunks reserved and commited using VirtualAlloc. Then it uses various algorithms and structures to use this memory as efficiently as possible.

I tested allocation of 2 bytes, and from debugger I see that it first pads it to 4 bytes, and puts it into SmallBins - which is a way of handling small memory allocations. In the end I see real memory usage of this allocation is 16bytes. But this is probably platform dependant.

marcinj
  • 48,511
  • 9
  • 79
  • 100