CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2006
    Posts
    98

    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 somebody enlighten me? Thanks!

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Stack overflow when declaring array

    Code:
    struct MyStruct
    {
        int u;
        int v;
        int w;
        int x;
        int y;
        int z;
    };
    
    class MyClass
    {
    private:
        MyStruct my_array[1000][500];
    };
    
    MyClass my_class;
    
    void main()
    {
        
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Stack overflow when declaring array

    This question has already been answered elsewhere, and frankly, a global variable is typically a non-solution.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Aug 2006
    Posts
    98

    Re: Stack overflow when declaring array

    Great that seems to work, thanks.

    One more thing - what's the difference between these two in declaring my_class:

    Code:
    MyClass my_class;
    
    // or
    
    MyClass my_class = new MyClass();
    Are they the same? And is the only difference that if I wanted to have a constructor with parameters, I could use the latter to initialize some variables??

    Thanks.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Stack overflow when declaring array

    Quote Originally Posted by ejohns85
    Are they the same?
    Effectively, since the second version is likely to be optimised to the first, but if I am not wrong, in theory the second version involves default construction of a temporary and then copy construction of my_class.

    Quote Originally Posted by ejohns85
    And is the only difference that if I wanted to have a constructor with parameters, I could use the latter to initialize some variables?
    No, you can directly initialise with arguments, e.g.,
    Code:
    MyClass my_class(x, y, z);
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Stack overflow when declaring array

    Quote Originally Posted by laserlight View Post
    This question has already been answered elsewhere,
    I was just about to post one like that!
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Stack overflow when declaring array

    Code:
    MyClass my_class = new MyClass(); // Looks like java!
    That won't compile.

    Did you mean
    Code:
    MyClass my_class = MyClass(); // A bit pointless
    or
    Code:
    MyClass* my_class = new MyClass(); // Manual memory allocation
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #8
    Join Date
    Mar 2009
    Posts
    51

    Re: Stack overflow when declaring array

    You could do this:

    Code:
    class MyClass
    {
    public:
        MyClass()
        :    my_array( 1000, std::vector<MyStruct>(500) ) // 1000 vectors with 500 MyStruct-s each.
        {
        }
    
    private:
    
        std::vector< std::vector<MyStruct> > my_array;
    };


    EDIT:
    Quote Originally Posted by laserlight View Post
    This question has already been answered elsewhere, and frankly, a global variable is typically a non-solution.
    Oh - well nicely confirmed then.
    Last edited by Zaccheus@Work; April 3rd, 2009 at 06:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured