Segmentation faults when initializing large arrays

This is a discussion on Segmentation faults when initializing large arrays within the C++ Programming forums, part of the General Programming Boards category; Hi! I have come across some weird behavior the other day which results in unexplained segmentation faults. Let's say I ...

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    Segmentation faults when initializing large arrays

    Hi!

    I have come across some weird behavior the other day which results in unexplained segmentation faults. Let's say I have a program which reads:

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class SomeClass
    {
    };
    
    void someFunc()
    {
        vector<SomeClass*> HugeArray[1000][1000];
        HugeArray[0][0].clear();
    }
    
    int main()
    {
        someFunc();
        return 0;
    }
    It compiles fine, but then sigsegvs on me upon entering someFunc (when control has not even yet reached the clear()-call). But if I make the function global, it runs perfectly fine:

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class SomeClass
    {
    };
    
    vector<SomeClass*> HugeArray[1000][1000];
    
    void someFunc()
    {
        HugeArray[0][0].clear();
    }
    
    int main()
    {
        someFunc();
        return 0;
    }
    It also runs fine without making HugeArray global, but then I have to reduce its size to f.ex. 100x100.

    Is this a compiler-related problem? I have tried this on GCC 4.x

    Thanks in advance
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    This has been discussed numerous times in the last week or so. Stack size is much more limited than the heap. Massive memory declarations should be one of the following:

    • static and local
    • global
    • allocated dynamically


    I generally recommend the last option, however, there is nothing wrong with the first one, and in fact, it might even be preferred in some cases. Global variables in general, though, are a bad idea when you can avoid them.

    Edit: For clarification, local variables are generally allocated on the stack while dynamically allocated memory comes from the heap. Static local variables and global variables are usually built right into the executable.
    Last edited by MacGyver; 07-14-2008 at 04:51 AM.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    FYI, you are abusing vectors. You want a 2D array, not an array OF vectors.
    So then, you want a vector of vectors:
    std::vector< std::vector<SomeClass*> >
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Quote Originally Posted by MacGyver View Post
    This has been discussed numerous times in the last week or so.
    Oh, sorry, didn't know that And thanks for the detailed explanation!


    Quote Originally Posted by Elysia View Post
    You want a 2D array, not an array OF vectors.
    No, I actually want a 3d array with limited, predefined x and y-dimensions and an unlimited z-dimension. That is why I have a predefined 2d-array of vectors
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by MathFan View Post
    Oh, sorry, didn't know that And thanks for the detailed explanation!
    I mentioned it more along the lines of a suggestion that you might glean more information from the recent discussions on the subject since people might not be willing to re-explain the same thing at the same level of detail each time. Wasn't meant to condemn you, although searching the forums before posting is a good practice, I do admit.

    Hope this was helpful.

  6. #6
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Quote Originally Posted by MacGyver View Post
    I mentioned it more along the lines of a suggestion that you might glean more information from the recent discussions on the subject since people might not be willing to re-explain the same thing at the same level of detail each time. Wasn't meant to condemn you, although searching the forums before posting is a good practice, I do admit.

    Hope this was helpful.
    Yes, I am sorry, my fault. I do tend to get carried away asking questions while forgetting to try finding the answer myself.

    And again, thank you for taking the time to respond to my question despite that
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-03-2009, 04:40 PM
  2. Replies: 5
    Last Post: 08-14-2007, 10:34 AM
  3. Replies: 4
    Last Post: 01-30-2005, 03:50 AM
  4. How large can arrays be?
    By overspray in forum C++ Programming
    Replies: 9
    Last Post: 07-21-2004, 02:25 PM
  5. Large Arrays
    By xurumin in forum C++ Programming
    Replies: 7
    Last Post: 08-26-2003, 05:17 PM

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