Stack Overflow when declaring array

This is a discussion on Stack Overflow when declaring array within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a class, MyClass, which contains a 2-dimensional array, my_array, of a structure of type MyStruct. Below is ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Stack Overflow when declaring array

    Hello,

    I have a class, MyClass, which contains a 2-dimensional array, my_array, of a structure of type MyStruct. Below is the code I have used to implement this :

    Code:
    struct MyStruct
    {
        int u;
        int v;
        int w;
        int x;
        int y;
        int z;
    };
    
    class MyClass
    {
    private:
        MyStruct my_array[1000][500];
    };
    
    void main()
    {
        MyClass my_class;
    }
    However, when I compile this, I get a compiler error indicating "stack overflow". I'm a bit of a rookie, but from what I gather this is because when I create the my_struct array, I am creating it directly onto the stack, rather than the heap. And, seeing as my_struct contains a large number of MyStructs, each of which contains 6 ints, then that is a lot of data to be stored on the stack, causing an overflow.

    But I don't know how to do this any other way, and I don't really understand which elements of the program are created in the stack, and which are created on the heap.

    I have tried defining MyClass without telling it to explicitely create all of the elements of the array:

    Code:
    class MyClass
    {
    private:
        MyStruct my_array[][];
    };
    But I get the error: "error C2087: 'my_array' : missing subscript"

    All I want to do is let my program know that this array will exist, but I don't need to create all the elements of the array right at the start of the program do I? And they certainly shouldn't be put on the stack, as I will need to refer to them throughout the program...

    This whole stack / heap thing is getting me down, please could

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    An array of this size needs to be allocated dynamically. That's what std::vector is for:

    Code:
    #include <vector>
    
    std::vector<std::vector<MyStruct> > my_array;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,793
    Quote Originally Posted by ejohns85
    All I want to do is let my program know that this array will exist, but I don't need to create all the elements of the array right at the start of the program do I? And they certainly shouldn't be put on the stack, as I will need to refer to them throughout the program...
    One possibility is to #include <vector> and then change your MyClass definition to:
    Code:
    class MyClass
    {
    public:
        MyClass() : my_array(1000, std::vector<MyStruct>(500)) {}
    private:
        std::vector<std::vector<MyStruct> > my_array;
    };
    By the way, void main should be int main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Any direct array declaration in a function does indeed use stack-space. That's the whole point of using such declarations (no need to call new or delete to arrange cleanup, etc).

    Since 500 * 1000 elements of 6 integers is likely to be 4 * 6 * 500 * 1000 = ~12MB of space, it will overflow the stack. The normal size of stack is 1-4MB - it can be made larger, but likely is that you do not want to use stack-space for such variables anyways.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers - stack dumping
    By steve1_rm in forum C Programming
    Replies: 3
    Last Post: 06-04-2009, 10:21 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. stack overflow
    By Unregistered in forum C Programming
    Replies: 29
    Last Post: 08-05-2002, 02:57 PM
  4. FloodFill stack overflow
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 04-09-2002, 04:21 PM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21