declaring a multidimensional array

This is a discussion on declaring a multidimensional array within the C++ Programming forums, part of the General Programming Boards category; Hi all, I would like your help I am trying to declare a multidimensional arrays in the following way: int ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    declaring a multidimensional array

    Hi all,

    I would like your help

    I am trying to declare a multidimensional arrays in the following way:

    int PSF[57][57][65][65];

    The full code has others variables.

    I compile without problems, but when I run the program, appear a error:
    Segmentation fault

    I do not know what I am doing wrong ...


    Someone know how solve this problem..???

    Thanks

    Manuel

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,807
    The likely problem is that 57 * 57 * 65 * 65 * sizeof(int) is too much space to allocate on the stack. You can use a std::vector instead to avoid this.
    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

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Hi laserlight,

    Thanks for your help


    Can you help me?

    I am new in C++, so I do not know how declare a std::vector with this size, can you indicate me how do it??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,807
    It might be along the lines of:
    Code:
    typedef std::vector<std::vector<int> > vector2d;
    typedef std::vector<vector2d> vector3d;
    typedef std::vector<vector3d> vector4d;
    
    vector4d PSF(57, vector3d(57, vector2d(65, std::vector<int>(65))));
    Of course, remember to #include <vector>.
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    33,292
    The really simple answer is to just make it static.

    static int PSF[57][57][65][65];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multidimensional array - newbie help
    By trixxma in forum C Programming
    Replies: 6
    Last Post: 03-25-2006, 05:32 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. multidimensional array question
    By Mr_Jack in forum C++ Programming
    Replies: 10
    Last Post: 11-01-2003, 04:54 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. help with declaring array in which the user specifies size
    By ibanezrgking in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 09:05 AM

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