Setting the stack size

Discussion in 'Mac Programming' started by jymer, Dec 9, 2011.

  1. macrumors newbie

    Joined:
    Dec 9, 2011
    #1
    Hi,
    I'm trying to define a >64 MB array in C and compiling with GCC. It compiles OK, but then seg faults "Segmenation Fault: 11". I read else where that this is because the default stack size (kb) with 8192. I tried bumping this limit up using ulimit, getrlimit, and a few others and it won't permit me to grow it beyond 64 MB. Also, setting stack_size in the linker fails for anything above 64 MB.

    I come from Linux, and am new to Mac. My experience with Linux is that stack size is limited to something like the physical memory size and I've never had a problem with GCC like this before. There's something I'm missing here with Mac, but why can't I make an array > 64 MB? is there an easy solution that doesn't involve recompiling the kernel?

    I'm running Mac OS X 10.7.2, gcc 4.2.1 that is distributed with Xcode.

    If anyone could help here, I would be grateful.
    Thanks.
     
  2. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    Sure.

    Code:
    void someFunc(size_t size) {
       char *array = (char *) malloc(size); // size could be 64000000
       // do stuff with array I'm in the stack!
       free(array);
    }
    
    Why on earth would you want anything on the order of a 64MB stack frame?!
     
  3. thread starter macrumors newbie

    Joined:
    Dec 9, 2011
    #3
    That did it! Wow, I feel dumb. You rule.

    Honestly, I only knew in theoretical terms what the 'stack' was so I was pretty willy-nilly about memory usage. Only now do I realize the huge arrays I create don't necessarily have to live in the stack. For my research I regularly manipulate data sets of > 1GB, and I got a Macbook Pro with 8GB memory and 64-bit OS thinking I could start to do some of this outside of my cluster at work.

    Thanks again.
     
  4. macrumors 6502a

    Joined:
    Jan 15, 2007
    #4
    Just an FYI, ulimit worked on OS X ver 10.4 and lower, but for whatever reason they removed it starting with Leopard...
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    I don't understand this. The ulimit shell command works the same as it does in 10.4. I just tried it in bash on both 10.5 and 10.6 and it's the same as it always was. Some hard limits changed (increased), and some default soft limits also changed after 10.4. The command is identical, though.

    Example command lines:
    Code:
    ulimit -Sa  # soft limits
    ulimit -Ha  # hard limits
    
    The system calls behind the ulimit commands are getrlimit() and setrlimit(). Read the man page for getrlimit.
     

Share This Page