Thread: Arrays failing to work

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    10

    Arrays failing to work

    Hi all,

    I'm having a huge issue over a simple problem. I'm trying to initialize two arrays which are fairly large.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
     unsigned short Projections[336][265][283];
     unsigned short Michelogram[18][18][336][283];
    
     printf("\n-=<This program ran successfully>=-\n\n");
    
     return 0;
    
    }
    This compiles fine, but when I run it, I get:
    Segmentation fault (core dumped)

    I've been compiling this snippet on gcc 3.3.2 in Red Hat Linux Fedora core 1. I haven't compiled this on Windows Bloodshed Dev-C++ yet.

    What confuses me is that this code below works:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
     /*unsigned short Projections[336][265][283]; */
     unsigned short Michelogram[18][18][336][40];
    
     printf("\n-=<This program ran successfully>=-\n\n");
    
     return 0;
    }
    By getting rid of one array, and toning down the other, it works.

    It's almost as if these arrays have a maximum size, or something to that effect. At first, I thought this was a memory issue. The computer I'm running it on is a P3 300MHz w/ 128MB SDRAM. I compiled it on a Dual Xeon 2.4GHz with more RAM, but I still got the same error.

    Anyone else have this problem?

    Any suggestions would be extremely helpful.

    Cheers,
    Bri Rock
    Last edited by Bri Rock; 06-14-2004 at 12:45 PM.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Linux limits the amount of memory one user can use, try it as root, if it still doesn't work, you know that is not the problem because root has not memory restrictions. If it does, you need to edit something in /proc (can't remember offhand) that changes the rlimit of the memory max a user can have.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You may be exceeding the maximum stack size. You can try allocating your memory dynamically... or putting the array declarations above the main function to make it global. Assuming 16-bit shorts and no math mistakes on my part, the Projections takes up 48 Megs of space and Michelogram takes up 58 Megs.

    I ran your initial code under MSVC++ 6.0 and it complied and linked fine but bombed out. Moving the array declarations above the main function like I said fixed the problem.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could always make them static, if you also want to preserve the scope as well.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    10
    Hey guys,

    Thank you very much for all the sugesstions!

    All of these solutions worked.

    The full program now works great. Thanks again, and maybe I can help out in return for the favor.

    Cheers,
    Bri Rock

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 06:29 PM
  4. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  5. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM