Deaclaration of 3D variables with C++

Comments

2 comments

  • Eli Towle

    Hi Ali,

    Your arrays are too large to fit on the stack. By using sizeof, we see that each GRBVar element requires 8 bytes of memory. With \( n=39 \), your three GRBVar arrays require \(8(n^2 + 2n^3)\) = 961,272 bytes, or ~0.96 megabytes (one 2D array and two 3D arrays). With \( n = 40\), these arrays require 1,036,800 bytes, or ~1.04 megabytes. This suggests that your stack size is 1 megabyte.

    Instead, you could allocate these arrays on the heap. E.g., for a three-dimensional array:

    GRBVar ***x = new GRBVar**[n];
    for (int i = 0; i < n; i++) {
        x[i] = new GRBVar*[n];
        for (int j = 0; j < n; j++) {
            x[i][j] = new GRBVar[n];
        }
    }

    Be sure to free this memory once it's no longer needed.

    Thanks,

    Eli

    1
    Comment actions Permalink
  • Ali Balma

    It works.

    Many thanks Eli.

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk