Thread: segmentation fault when using command line arguments

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Medellin, Colombia
    Posts
    2

    Question segmentation fault when using command line arguments

    Hi, thanks for reading. This is my first post so I hope I used the tags right.

    The problem is simple. I need to use huge arrays and to get arguments from the command line, however, when I try to use both at the same time I get a segmentation fault.

    This is the code I'm using


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DIMR 1024 
    #define DIMANG 50
    #define DIML 8
    
    int main(int argc, char *argv[]){
    	double psi[DIMR][DIMANG], f[DIMR][DIML+1], g[DIMR][DIML+1], g_op_fact[DIMR], f_op_fact[DIMR][DIML+1], g_update_fact[DIMR][DIMR], f_update_fact[DIMR][DIMR];
    	
    	return 0;
    }
    when I change "int main(int argc, char *argv[])" to "int main()", the segmentation fault goes away. Also, if use not so big arrays (#define DIMR 512) with "int main(int argc, char *argv[])" the segmentation fault disappears.

    I don't really have a clue about how to fix this. Any help would be much appreciated

    PS: I have 3Gb of RAM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Your arrays are too large and are blowing the stack. Stack size is limited. If you need arrays that large you need to look into dynamic memory allocation via malloc/calloc.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by rags_to_riches View Post
    Your arrays are too large and are blowing the stack. Stack size is limited. If you need arrays that large you need to look into dynamic memory allocation via malloc/calloc.
    Or you could use static memory either by prefixing those variables with a "static" keyword or placing them in the global scope.

    When your application would have more code in it and would run for a longer time, then it would be a good idea to use dynamic memory allocation for arrays that you only use for a short time in the application and static memory for arrays that you use throughout the program. However, if the exact amount of required array size is not known, then you should also go for dynamic allocation.
    Last edited by maxorator; 08-27-2010 at 05:26 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Those arrays are huge. You're giving yourself a stack overflow.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To summarize: the way you've declared those arrays puts them on the stack, which has a limited size (never mind how much physical memory your system has). When you declare main as "int main()", the stack is set up differently and this may seem to make it work -- but since you're putting so much data on the stack it's going to blow up eventually.

    The best way to get around this is to allocate the memory on the heap. The easiest way to do this is with malloc() or calloc(). Programs as a matter of course often allocate many megabytes of memory this way, so your requirements shouldn't be an issue. It would be even better if you could determine at runtime how much memory you need, exactly, rather than putting a safe upper limit like 1024 (assuming that's what it is); malloc() allows you to allocate precisely the amount of memory that you need.

    The hackish way around this is to declare the arrays as global (put them outside any functions) or static (prefix the declaration with the "static" keyword). This does not put the data on the stack or the heap, but rather another special segment sometimes called the "static data segment". This can support more data than the stack, and it's intended for this sort of thing. (Once the program starts, nothing is typically added to the static data area, so you can fill it with whatever is necessary. But the stack is used for function calls and to support recursion, so by putting a large amount of data on it you could hamper your program's capabilities later on.)

    So start reading some tutorials on malloc() . . .
    e.g. Cprogramming.com Tutorial: Pointers
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Medellin, Colombia
    Posts
    2
    Thanks to all!! and specially to dwks for the very informative answer. By the way, the reason I need these arrays is the fact that the program I've just "finished" (thanks to all of you) is intended to simulate a hydrogen atom and the algorithm used relies on the number of points in a 3dimensional grid, so the more points the better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

Tags for this Thread