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
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,310
    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>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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