#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    can i declare a variable -> array[256][1024]?


    I want to declare an multidimensional array as follows:

    array[256][1024]....
    but the program seems to crash.

    when i use smaller numbers like
    array[50][50]

    the program runs like its supposed to..

    can i use 256 and 1024?
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    1
    Rep Power
    0
    What type is the array?

    What is the size of each element in the array?
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Are you declaring it inside of a function? If so, you are restricted to the stack size. Also remember that int main() is a function, as well! Take at look at this thread, in which we discussed all of this before.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    13
    Rep Power
    0
    i got it up to 500...here is the code:

    #include <iostream.h>
    int main()
    {
    int theArray[500][500] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}};
    for (int i = 0; i<500; i++)
    for (int j=0; j<500; j++)
    {
    cout << "theArray[" << i << "][" << j << "]: ";
    cout << theArray[i][j]<< endl;
    }
    return 0;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Bangalore, India
    Posts
    13
    Rep Power
    0
    The program crashes because it exceeds the size of memory heap.
    When you declare a multidmensional array, the program reserves some space. When the size of array is large enough, it runs out of space and hence gives an error.
  10. #6
  11. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    bes, you are delcaring it inside of a function (main() is a function, too). Declare it outside the function, as a global variable, and you will not be restricted by the stack space, which is normally 256 Kb (even though it sounds like its 1,024 Kb in your case - it can be changed with compiler settings). The global heap space is limited to 256 Mb for older 32-bit OS's, and limted only to 4 Gb for newer 32-bit OS's. Take a look at my first post above.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    13
    Rep Power
    0
    lol..sorry im new at this stuff...I just got that outta my C++ book
  14. #8
  15. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    There is no need to be sorry... I just want to make it clear to everyone that there is a difference between declaring a variable inside of the main() function, and declaring it globally, outside of the main() function. This is a problem that caught me off guard, as well, so I know it will catch a lot of other people... I guess it is easy to think that you are not declaring variables on the stack when you declare them at will anywhere inside of main(), because it just does not seem like a function for some reason.

    I should also note that dynamically allocating the arrays at run-time is another method of using all possible RAM with no complaints from the compiler.

IMN logo majestic logo threadwatch logo seochat tools logo