9

I have a question involving memory allocation. Let's say I create an array of pointers like this.

int **numbers = new int *[1024*1024];

I had assumed that this would need 8MB of memory (8-byte pointer on Mac 64-bit) but that's not the case. Memory is only allocated when each pointer is assigned a value. So if I NULL all the pointers then I see 8MB being allocated.

for(int i=0; i<1024*1024; i++)
{
    numbers[i] = NULL;
}

How does my app know which pointers have an assigned a value without allocating memory for it?

Berry Blue
  • 15,330
  • 18
  • 62
  • 113
  • 9
    The `new` is probably just reserving address space, and only committing memory once the memory is written to. This is a common optimisation, and is performed by the OS. – Mankarse Jun 26 '13 at 15:03
  • Just to add to the confusion, if you were to read, instead of write, all of the pointers, they would all be NULL, but in this case your memory would not go up, because you would just be reading the zero page over and over again. – Neil Jul 10 '13 at 12:18

3 Answers3

12

From C++ perspective your memory is allocated and there. You better not confuse yourself by what OS is reporting.

Your "app" definitely does not know whether it assigned a pointer or not -- just you as the programmer are responsible to not use whatever pointers before assignment.

If curious on possible backgroung, look around for memory overcommit like here or in other articles.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
5

You are observing a feature of the OS, called overcommit-accounting.

Unassigned memory only gets reserved address-space (process specific virtual memory) by default. Only when assigning values to it, the pages are actually mapped to physical addresses in the page-table.

mirk
  • 5,302
  • 3
  • 32
  • 49
2

My guess is memory is allocated , But not showing in the ps/top result [not sure which tool in mac] as the pages hasn't been touched . chk all the memory related columns

Anand Rathi
  • 790
  • 4
  • 11