double *d = new double[N]

Nov 10, 2012 at 8:48pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main() {

  cout << "program started..." << endl;


  const int N = 1e6;

  for (int i = 0; i < N; ++i) {
    double *d = new double[N];
  }
  
  return 0;
}


Hello, could you explain me what this program does, it i hope locates one billion arrays in the dynamic memory and each of this billion arrys has one billion places for "int" variables?

Thanks in advance :)
Nov 10, 2012 at 9:32pm
It allocates a million double arrays that can each hold one million doubles.
Theoretically.

A double requires 8 bytes, so you try to allocate...
8 * 106 * 106 = 8 terabytes.

Do you have that much memory?
Nov 10, 2012 at 9:53pm
Funny thing is, it passes the address for each array to the same pointer. So you are leaking the memory for all but the last array.
Nov 10, 2012 at 10:27pm
Please do delete[] all new[] memory.
Allocating memory without deallocating consumes your resources. You have a memory leak.
Topic archived. No new replies allowed.