tracking a segmentation fault

From: Koen (kvddrift_at_earthlink.net)
Date: 10/31/03


Date: 31 Oct 2003 11:12:18 -0800

Hi,

I am trying to track a segmentation fault (EXC_BAD_ACCESS) that's
happening in a program I use on Mac OS X (not my own code). The reason
for the fault is that the default stack on Mac OS X is small. If I
call 'unlimit stack' before I start the program, there is no error.
However, I would like to avoid typing ulimit stack every time.

Examining the code indicates that 2 large static arrays are defined
outside main() as well as many array's inside main():

static int myarray[20][10000];
static int anotherarray[20][10000];

int main (int argc, char **argv)
{
        int labels[20];
        int names[20][10000];
        float num[20][10000];
        .....

So, after some reading, I understand I can add something like this to
the code, to increase the stack:

struct rlimit rlim;
getrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = RLIM_INFINITY;
setrlimit(RLIMIT_STACK, &rlim);

Unfortunately, even if I put this right after int main (int argc, char
**argv), I still get the error. If I use gdb, I immediately drop in
the error after main() starts. So I suspect the compiler is creating
all the array's first, using all the available stack.

Is there a way to modify the code so that the 'rlimit' snippit is
called before everything else happens? Or maybe I need to use
different compiler settings?

thanks,

- Koen.



Relevant Pages

  • A basic C question about segmentation fault
    ... int main ... This program terminate just after one loop while the second program ... I partially understand that in the second program the stack becomes ... In what all cases does segmentation fault occurs. ...
    (comp.lang.c)
  • Re: Having C code looking like C++ code
    ... providing an integer stack. ... | extern int push; ... | typedef struct stacktype { ... muck around inside the encapsulation to use the interface. ...
    (comp.lang.c)
  • Re: [OT] C programming, variable size array
    ... The trick to understanding here is to know how memory gets allocated. ... int main{ ... The stack holds "automatic" variables used in C, ... Will automatically allocate and deallocate 1K of data on the stack. ...
    (Debian-User)
  • Re: Link Instruction & tt command
    ... In order to do the trace, ... than C, the routine's entry point is non-standard, or the task's stack ... int f_c ... framepointer, and you can not make any stacktraces. ...
    (comp.os.vxworks)
  • Re: Resources about program design in C
    ... an interest in the reasoning behind global variables in C programs. ... For example, sorting an array is a single, well-defined ... imagine a module that implements a stack ... static int stack_ptr; ...
    (comp.lang.c)