problem initializing a double array for large array

This is a discussion on problem initializing a double array for large array within the C Programming forums, part of the General Programming Boards category; Hi, Silly question from a new C programmer... I get a segmentation fault in the following code: #include <stdio.h> int ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    problem initializing a double array for large array

    Hi, Silly question from a new C programmer... I get a segmentation fault in the following code:

    #include <stdio.h>
    int main(void)
    {
    double YRaw[4000000]={0};
    return 0;
    }

    Using GDB, I get the following comment:

    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5dd7b148
    0x0000000100000f24 in main () at talk2me.c:18
    18 double YRaw[4000000]={0}; // set YRaw[memdepth] so index is 0 to memdepth-1

    Everything works find if I reduce the size of the YRaw array by a factor of 10. I have 6GB of RAM in the system, so why do I get an error? Thanks, Gkk

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    sizeof(double) * 4000000 might be too much so that causes stackoverflow.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Do ulimit -s (ulimit -a to see all the ulimit options) on the command line - this will tell you your max stack size (In kilobytes - also, this cap can be removed) for your system.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Local variables are stored in stack. Stack size is usually limited and the limit is not usually high. So there are 3 good ways to store large arrays: using the static keyword to make the memory static (usually initialized to zero at first, will keep its values from the previous execution of the function), making them global (also keeps values and usually initialized to zero), or allocating them dynamically (for example with malloc and free, always contains uninitialized "junk" data after allocation).
    Last edited by maxorator; 08-25-2010 at 09:28 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Replies: 17
    Last Post: 11-22-2008, 03:40 AM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 09:38 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. problem initializing an array
    By ArseMan in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2001, 10:04 PM

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