double *d = new double[N]

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 :)
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?
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.
Please do delete[] all new[] memory.
Allocating memory without deallocating consumes your resources. You have a memory leak.
Topic archived. No new replies allowed.