segfault when allocating big matrix

This is a discussion on segfault when allocating big matrix within the C Programming forums, part of the General Programming Boards category; Why the program below generates a segmentation fault? 100.000 x 26 should occupy 3MB at most... is it too much? ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    3

    segfault when allocating big matrix

    Why the program below generates a segmentation fault? 100.000 x 26 should occupy 3MB at most... is it too much?

    Thanks.

    Code:
    #include <stdio.h>
    
    int main() {
    
        char m[100000][26];
    
        return 0;
    
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    No idea why. It does run normally for me. Use malloc though if you don't think of anything else.

    Code:
    char **m = malloc(100000 * sizeof(char *));
    for (i=0; i < 100000; i++)
       m[i] = malloc(26 * sizeof(char));
    Last edited by C_ntua; 06-15-2008 at 06:32 PM.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Seems to me default stack size if usually around 1MB (although a Google search indicates 8MB for OS X), so best bet is you're exceeding your stack limit. This is also usually a configurable value, so you may be able to change it to be more to your liking. Or you can go with the heap, as C_ntua suggests, and which would be more portable.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Why the program below generates a segmentation fault? 100.000 x 26 should occupy 3MB at most... is it too much?
    In the wonderful world of protected mode programming it's not too much. However something tells me there are better data structures out there that you can use to gain the same functionality.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by lecter55 View Post
    Why the program below generates a segmentation fault? 100.000 x 26 should occupy 3MB at most... is it too much?

    Thanks.

    Code:
    #include <stdio.h>
    
    int main() {
    
        char m[100000][26];
    
        return 0;
    
    }
    Yes it is too much. The default stack size is usually 1MB and if you ever find that this is not enough then basically you're doing something wrong. You need to use dynamic memory allocation.
    Note that the suggestion of 100000 allocations of 26 bytes each is not particularly efficient. You should only alocate the entries in such an array that you are actually using. Also you shouldn't attempts to declare something huge as a catch-all size and cross your fingers that it is big enough. Always only allocate approixmately how much you need and if you need more later then allocate one twice as big and copy the old items across.

    Better yet, use C++ and use a std::vector that will do this for you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

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