what is the major difference with block allocation vs individual object allocation. let say
int iCount = 5;
int i = 0;
while(i < iCount)
{
f = new foo();
i++;
}
//////////////////
foo* f = new foo[iCount];
will that second method save some memory space for me?.
i heard that every object we allocated surrounded by 16 admin bytes. so block allocation will only using one header guard. is it true?.
Every allocation you do also allocates also allocation header (sometimes also some footer guard structure), which is dependent on algorithm used by your allocator. Here, you can find description of one of such algorithms.
When you allocate an array, the allocator (mainly
malloc()) will be called withsizeof(element) * countas argument and will allocate entire array as one block, using one header struct, so it will introduce less memory overhead than allocating elements one-by-one (see note at bottom).Anyway (as the question is tagged with c++), good C++ programmer should avoid managing memory manually. For array, use Standard Library classes (
vector,map,listetc.). Use RAII when possible, don’t use raw pointers, but “smart” ones.Note: everything I wrote here is entirely dependent on algorithm used, so that paragraph about array allocation may not apply to all possible memory allocation algorithms. So, direct answer to “what is the mayor difference between block allocation vs individual object allocation” is also algorithm-dependent.