multi-dimensional arrays

This is a discussion on multi-dimensional arrays within the C++ Programming forums, part of the General Programming Boards category; i declared an array x[300][300][10] and it gave a segmentation fault. Is it do to with memoery allocation because it ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    multi-dimensional arrays

    i declared an array x[300][300][10] and it gave a segmentation fault. Is it do to with memoery allocation because it worked for smaller sizes x[30][30][3]?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Stack space can vary, but in general these days you should consider having about 1MB in total. 300 x 300 x 10 = 900,000 bytes. Don't allocate that much on the stack.

    Allocate it dynamically with new, or more properly, find a more suitable data structure that fits your needs. It appears you're being wasteful with memory.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Better yet, for arrays, std::vector is a great choice. It manages the memory dynamically for you.
    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
    Join Date
    Mar 2007
    Posts
    65
    can you have multi-dimensional vectors?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes. A vector of vectors.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    Can the blitz library do large multi-dimensional array operations?

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    Or else, How do you change the stack size emory?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    If you want just one large array, and you don't want to mess about with dynamic memory, then make it global or static.
    Code:
    int array[1000];
    int main ( ) {
    }
    
    or
    int main ( ) {
        static int array[1000];
    }
    Each system has different ways of setting the stack size, so it's best avoided if at all possible.
    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. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. fscanf for multi dimensional arrays
    By rambos in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 03:26 AM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM

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