Thread: multi-dimensional arrays

  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,210
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,210
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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, 03: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, 04: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