CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 20

Threaded View

  1. #16
    Join Date
    Apr 1999
    Posts
    27,449

    Re: help with complex struct (or class)

    The item takes up a lot of stack space. In Visual Studio 2008, a checking of the stack is done when the vector gets created, and a "stack overflow" error occurs. That's probably the reason you are having problems.

    The struct is, give or take, around 800 bytes. So by the time you get to the Order struct, you need around 8 megabytes of stack to accommodate the Order struct.

    There are various solutions:

    1) Make those arrays inside the struct vectors also OR

    2) Allocate the Order from the heap instead of making it a global object OR

    3) Make it a std::vector<Order*> and dynamically create the Order using "new Order", and place that in the vector:
    Code:
    std::vector<Order*> order(4);
    //...
    for (int i = 0; i < 4; ++i )
       order[i] = new Order;
    //...
    //.. delete them when you're done.
    for (int i = 0; i < 4; ++i )
        delete order[i];
    Smart pointers are an alternative to the above, but is essentially the same thing.

    There are other solutions, but these three are the ones that are most prominent in my mind at the moment.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 1st, 2009 at 07:53 PM.

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