-4

I am trying to run a simple code that will create a 2D array. At first I declared the array normally, but it got an abnormal termination.

In the task manager the program typically grew to a size of 0.4 MB and was terminated. So, after some research, I discovered the heap and consequently tried to store there. This time it took almost 3 GB and my OS restarted due to low memory.

int main()
{
    for(int i=0;i<1000;i++)            // outer loop to create 1000 instances
    {
        int** c = new int*[3000];         // creating 2D array of size 3000*3000
        int** b = new int*[3000];

        for(int z=0;z<3000;z++)
        {
            c[z] = new int[3000];
            b[z] = new int[3000];
        }

    }
    return 0;
}

So, as far as I can tell, the maximum size of c is [3000][3000] = 36*10^6 B = 36 MB

Now I have 4GB of RAM, so this seems quite unfair.

Anyway I understand that C++ does not have auto garbage collection; that's why the remains are left out. However, I need this array structure for computation. How can I get it to use more of the available memory?

TylerH
  • 20,799
  • 66
  • 75
  • 101
lu5er
  • 3,229
  • 2
  • 29
  • 50
  • sorry, I meant it. Just wrote wrong! – lu5er Apr 01 '17 at 15:59
  • `for(int z=0;z – ikh Apr 01 '17 at 15:59
  • 1
    Please remove `C` tag, because this has nothing to do with C. Memory you need is actually `2 * 3000 * 3000 * z * n * sizeof(int)` which is surely more than `36MB`. – user7771338 Apr 01 '17 at 16:00
  • If you want to put memory back for reuse, why not `delete` it? – user4581301 Apr 01 '17 at 16:02
  • @FREE_AND_OPEN_SOURCE Don't get you. – lu5er Apr 01 '17 at 16:02
  • @user4581301 Delete did not work too. Tried. – lu5er Apr 01 '17 at 16:03
  • `c[z] = new int[n];` There also is unresolved identifier `n`. Please make sure that your code is compilable – ikh Apr 01 '17 at 16:03
  • Then you used it wrong. Please post what you tried. – user4581301 Apr 01 '17 at 16:03
  • It's unclear what you're asking. What exactly are you expecting from us? – Captain Obvlious Apr 01 '17 at 16:03
  • @Apy. How you didn't get me? You are `36,000,000 * z` times allocating array of integers of size `n`. – user7771338 Apr 01 '17 at 16:04
  • [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Whatever you are learning C++ from is not doing you any favours. – user4581301 Apr 01 '17 at 16:05
  • Is this an Apri 1st joke? – user7771338 Apr 01 '17 at 16:05
  • @FREE_AND_OPEN_SOURCE Thanks for the reminder. I need to buy a new bus pass. – user4581301 Apr 01 '17 at 16:06
  • @Apy - You allocate a LOT of arrays, but never free them. Sure, at most it's 36MB in one go, but thanks to `for(int i=0;i<3000;i++)` you do this at least 3000 times. So that's 3000*36MB = 108GB. And then there are all the other (not-max) sizes too that you never free. Also, where does `n` come from and how big is it? – Vilx- Apr 01 '17 at 16:07
  • 3000 ( for i loop) 1500*3001*4(4 bytes per pointer(32 bit machine) and arithmetic progression of amount of memory allocation)* 2 (two aarrays c and b) (for j loop) k is unknown but if it is 3000 too, then 3000*2*4 (3000*1500*3001*4*3000*8/1024)/1024=1236373901 GB – AquaAsh Apr 01 '17 at 16:08
  • Why are you even allocating memory in a loop like that? – Vilx- Apr 01 '17 at 16:08
  • @Vilx-I had to implement a[3000][3000]. But stack memory won't let me do it. So, I had to do that for heap allocation. – lu5er Apr 01 '17 at 16:09

2 Answers2

2

Looks like you've got the wrong idea of how new works (and how to use it). Basically, your code is allocating way more than 3000 x 3000 integers.

That aside, I'd suggest collapsing the 2D array into a 1D array. I'd also suggest using std::vector<int> instead).

If you're set on allocating the memory yourself though you can use:

int* array = new int[3000 * 3000];

And then eventually call delete to free the memory as in:

delete[] array;

Here's an example:

int main()
{
    int* array = new int[3000 * 3000];
    // assume that array now points to un-initialized memory
    // i.e. don't assume any array element has any particular value
    //    until you set the value of the element.

    for (int i = 0; i < 3000; ++i)
    {
        for (int j = 0; j < 3000; ++j)
        {
             // To store a value...
             int yourvalue = i * 3000 + j; // or whatever value it is you want to store
             array[i * 3000 + j] = yourvalue;

             // Or to read a value...
             yourvalue = array[i * 3000 + j];
        }
    }
    delete [] array;
}

Hope this answers your question of how to create and use a 3000 x 3000 sized buffer of memory without consuming too much memory. Of course this assumes your system has at least 3000 * 3000 * sizeof(int) of memory to space. Which, if sizeof(int) == 4 (and it often does), is roughly 36MB.

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
1

You have 2 * sum{ 0 ~ 2999 } = 2 * 2999 * 3000 / 2 = 8,997,000 int*s, and each int* points int[3000].

If assuming sizeof(int*) == 8 and sizeof(int) == 4, The size of the memory your program needs is 8997000 * (8 + 3000 * 4) = 108,035,976,000 bytes.

108,035,976,000 byte > 105,503,882 KiB > 103,031 MiB > 100 GiB. I think your machine don't support 100 GiB size of memory space.

ikh
  • 10,119
  • 1
  • 31
  • 70