This results in the fact that new int[1000] consums less memory than 1000 times new int
That's not true. It's quite the opposite.
for (int i = 0; i < 1000; ++i)
{ something = new int; }
will take 1000*sizeof(int) in memory while
something = new int[1000];
will take 1000*sizeof(int) + bookkeeping of new[] array size.
However, the second call is relatively faster and in a multi-threaded application, will also result in a adjacent block of memory being allocated while no such guarantee is made for the first method.
Wtf are you talking about? That's completly beside what you said earlier or I. Woohoo, you've discovered infinite loop, go have a cookie. And indeed, most implementation have a int stored at array[-1], but it's not mandatory and no one cares, 'cuz it still must be stored somewhere, there or elsewhere.
Didnt you get it? new char[0] => array of zero size => should be no mem consuming if one assume that new itself does not consum mem. The fact: your os allocates mem for saving information for every single allocated memory block. Even for blocks with the size of ZERO. Which means: many news => high memory consumption by the hidden information!
Got it now?
P.S: only tested under windows. Dont know how Unix/MacOS/BSD/Linux handles an allocation of zero size.
When the object being created is an array, only the first dimension can be a general expression. All subsequent dimensions must be constant integral expressions. The first dimension can be a general expression even when an existing type is used. You can create an array with zero bounds with the new operator. For example:
char * c = new char[0];
In this case, a pointer to a unique object is returned.
Second: Have you even read what I wrote? 'cuz you are trying to argue that new[] use more memory than new, which was my point exactly AND the inverse of what you first said
Nox:
This results in the fact that new int[1000] consums less memory than 1000 times new int
@mods please seperate this disscussion from the main topic.
@dorth
'cuz you are trying to argue that new[] use more memory than new, which was my point exactly AND the inverse of what you first said
Sorry but where did i argue that new[] use more memory?
EDIT:
because im tired to argue anymore, i wrote this test only for you. Maybe you believe facts (tested under win7 64 - first test ~80MB, second test ~200MB):
Every allocation from the a general purpose allocator (regardless of if it is allocated with new, new[] or malloc()) has to keep some book keeping information. Typically allocations are slightly more than the requested size. These two factors will cause N small allocations to waste more memory than 1 large allocation.
Of course a custom fixed size allocator can be used to avoid some of this waste, but that is not consistent with the situation presented above.
Nox wrote:Dont know how Unix/MacOS/BSD/Linux handles an allocation of zero size.
0 byte allocations are required by the C++ Standard to return a unique address. The amount of wasted space might change between implementations, but the fact that there is waste is inevitable for a conforming implementation.
Another issue to consider is that small allocations tend to fragment the heap. This can cause premature out of memory conditions, even if the memory is deallocated, or it can cause performance problems when the deallocated blocks on the heap are coalesced.