CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2009
    Posts
    63

    seg fault with mere creation of 2 3d arrays in a single object

    hello all,

    so i need to have a class that has two 3-dimensional arrays, i.e.

    class values {
    public:
    long totals[8][8][8];
    int averages[8][8][8];
    int data_points;
    }

    however, when i try to create an instance of this class, i immediately get a seg-fault; if i remove one of the 3-dimensional arrays, it does not seg fault, and after repeated trial and error i have determined that if i were to resize one of the arrays to 3x3x3, it does not segfault. does anyone have an idea why this is happening and how to fix it?? thanks!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: seg fault with mere creation of 2 3d arrays in a single object

    Impossible to say without more information. But here are three possibilities based on screw-ups I've seen before with some regularity:

    1) You are trying to cast these arrays to long***s somewhere. Despite the fact that a T[8] is convertible to a T*, the same does not hold true for multidimensional arrays.

    2) You are trying to create many of these objects and running out of stack space. You only have a couple MB on the stack, usually. (Creating one of these objects should be fine on its own.)

    3) You need to clean/rebuild your project, because some translation unit is confused about how large the values class is.

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: seg fault with mere creation of 2 3d arrays in a single object

    Post an example please. How do you create the class in question? How do you use it? Post an example that we can compile that demonstrates the problem. The arrays didn't seem that big to me but sometimes creating large arrays on the stack causes some run time problems. This might be one of those situations where you have to create the object in heap memory by using operator new.

  4. #4
    Join Date
    Jun 2009
    Posts
    63

    Re: seg fault with mere creation of 2 3d arrays in a single object

    yes, it looks like i may be running out of stack space, as i am trying to make multiple objects like this, and if i lower the dimensions (i.e. using less) it does begin to work. how can i get more memory on the stack?

    thanks!

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: seg fault with mere creation of 2 3d arrays in a single object

    Trying to get more memory on the stack is usually the wrong approach. Why don't you show us what you're doing so we can make targeted suggestions. It may be as simple as replacing this:
    Code:
    values v[5000];
    with
    Code:
    vector<values> v(5000);

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: seg fault with mere creation of 2 3d arrays in a single object

    or deque<values> d(5000), or list<values> L(5000) depending on what you need to do with the array or whether it will grow at run-time.

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