Setting the stack size

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

  1. jymer macrumors newbie

    Dec 9, 2011
    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.
  2. jared_kipe macrumors 68030


    Dec 8, 2003

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

    Dec 9, 2011
    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. foidulus macrumors 6502a

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

    Aug 9, 2009
    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:
    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