Take the 2-minute tour ×

I've been running tests using gprof on a simple version of a vector which allocates memory on the heap (without actually being dynamic - tests purposes only). The thing is that looking at the result I see there's a huge difference between the 'new[]' and the 'delete[]' - given that I actually insert values to the vector (using the [] operator). Doing the above, I got results like:

%   cumulative   self              self     total           
  time   seconds   seconds    calls   s/call   s/call  name    
  0.92      0.55      0.01        1     5.03     5.03  MyVector::~MyVector()
  0.00      0.55      0.00        1     0.00     0.00  MyVector::MyVector(int)

But If I just allocate memory and delete it, without actually inserting values to the vector, they work just as fast:

%   cumulative   self              self     total           
time   seconds   seconds    calls  ms/call  ms/call  name    
0.00      0.09     0.00        1     0.00     0.00  MyVector::MyVector(int)
0.00      0.09     0.00        1     0.00     0.00  MyVector::~MyVector()

My guess is that when using 'new[]' the compiler (gcc in my case) doesn't really allocate the memory, and only when it needs to it does that (like when using []). And when it needs to destroy the object, it has to de-allocate all the memory which was allocated during each access (using []).

I couldn't find any documentation for this - and maybe there's something I'm not aware of. I'd be happy if someone will share his knowledge regarding this issue.

Edit: I added the code I used. Thanks for all the answers so far:

class MyVector

        MyVector(int size) { _data = new int[size]; };
        ~MyVector() { delete[] _data; } ;

        int& operator[](int index) { return _data[i]; };

       int* _data;
       int _size;     

    And the test:

int main() {

      MyVector v(1000000);

      for (int j = 0 ; j<20000 ; ++j) {
          v[i]= i; //If i remove this line, destructor and constructor work just as fast

      return 0;
share|improve this question
can you show some code? –  Default Jan 17 '13 at 11:29
[] doesn't insert or allocate anyting.... –  Pubby Jan 17 '13 at 11:30
If calls 1 means you're only calling those functions once during your test, I'd question the value of those figures. Call them a few thousand times at least. –  Mat Jan 17 '13 at 11:31
what do all these numbers mean? –  Default Jan 17 '13 at 11:32
@Default - one answer is that the numbers are described here. Another is that the numbers are meaningless with a sample size of 1. –  Useless Jan 17 '13 at 11:35
show 2 more comments

1 Answer

You are correct. new[] does not really allocate memory immediately on all systems. On Linux for example, you get a pointer back that points to memory that will only get allocated if you try to actually use it. So:

something = new[N];
delete[] something;

where something is a pointer to a built-in type, is pretty much a no-op.

(This could be breaking standards conformance, because new is supposed to throw when no more memory is available (or return NULL in the nothrow version). But that's how it is; Linux pretty much treats this as if it had infinite memory.)

Note that this only applies to built-in types because they are not getting default-constructed with new[], unless you ask for it. Other types have their default ctor called, and thus the memory is accessed. In order to test, try just accessing the last element:

something = new[N];
something[N-1] = some_Value;
delete[] something;

Does this change your gprof results? If yes, then the delayed allocation is most probably the cause of what you're seeing.

share|improve this answer
Correct but meaningless. new[] does not allocate real memory, but it absolutely does allocate address space. And the expensive part is finding a contiguous chunk of address space. Real memory isn't mapped contiguously anyway, due to the MMU. –  MSalters Jan 17 '13 at 12:09
@MSalters Are you sure that address space allocation is not delayed until use? The pointer you get back initially can be mapped to anywhere later. –  Nikos C. Jan 17 '13 at 12:11
Unless the latest standard changed things (I haven't worked much in C++ lately), a call to the array-based new operator is required to invoke the default constructor for each element in the array. This requires that it not only find (or construct) a contiguous address space but that it also reference the new objects to construct them. That sounds a lot like allocation to me. –  D.Shawley Jan 17 '13 at 12:21
@D.Shawley Don't you only get that with something = new[N](); (note the () at the end.) –  Nikos C. Jan 17 '13 at 12:24
@NikosC. : absolutely. The pointer cannot be mapped to anywhere, because the pointer must point to an address range which cannot overlap any other address range. If 0x1040 is already allocated, new[] may not return 0x1000 for aarray of size 0x50. That would cause a 16 byte overlap. –  MSalters Jan 17 '13 at 12:29
show 3 more comments

Your Answer


