I was trying to explain the memory that has been taken to my application in Linux. I did a basic test and figured out that if we new some memory, it allocated at least 32 bytes for a single new.
This is my code.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, const char** argv)
{
int iBlockSize = atoi(argv[1]);
int iBlockCount = atoi(argv[2]);
for (int i = 0 ; i < iBlockCount ; i++)
{
cout << (int*)(new char[iBlockSize]) << endl;
}
return 0;
};
When I execute the ./a.out 8 100
it gave following result.
....
....
....
0xf6db10
0xf6db30
0xf6db50
0xf6db70
0xf6db90
0xf6dbb0
0xf6dbd0
0xf6dbf0
0xf6dc10
0xf6dc30
0xf6dc50
0xf6dc70
All the memory I got was having 32 bytes gap.
Till 24 (BlockSize) it was the same. If it goes beyond 24 it is 48 bytes.
./a.out 25 100
....
....
....
0x18b30c0
0x18b30f0
0x18b3120
0x18b3150
0x18b3180
0x18b31b0
0x18b31e0
0x18b3210
0x18b3240
0x18b3270
0x18b32a0
When I was testing this for larger sizes; figured out that the memory we are getting is increased by 16 bytes chunks keeping at least 8 byte overhead.
My questions are,
Memory allocation in C like languages must return memory suitably aligned [0] for all primitive types.
This means memory allocation will usually give you at least 8 byte aligned memory so you can store doubles
in them.
So when you request 1 byte of memory you will use at least 8 bytes due to the alignment requirement. On 64 bit systems memory allocation often gives you 16 byte aligned memory as these systems often have 16 byte large types (SSE vectors)
Additionally the memory allocator needs some space for its management data, like the size of the allocation. Depending on the implementation this data can be placed before or after the user allocated block of memory.
[0] Memory is aligned when the address/pointer is a multiple of the size it is accessed in, some cpus do not support unaligned access (e.g. sparc), others (like x86) can have performance penalties if you do so.
Few lines from C++ Standard related to the subject; I think it briefly describes that it is not wrong behaviour.
Any allocation and/or deallocation functions defined in a C++ program, including the default versions in the library, shall conform to the semantics specified in 3.7.4.1 and 3.7.4.2. ...
3.7.4.1 Allocation functions ... The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type with a fundamental alignment requirement (3.11) and ...
3.11 Alignment Object types have alignment requirements (3.9.1, 3.9.2) which place restrictions on the addresses at which an object of that type may be allocated. An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated. ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With