PDA

View Full Version : Setting the stack size




jymer
Dec 9, 2011, 02:29 PM
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.



jared_kipe
Dec 9, 2011, 03:17 PM
Sure.


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?!

jymer
Dec 9, 2011, 04:11 PM
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.

foidulus
Dec 9, 2011, 04:29 PM
Just an FYI, ulimit worked on OS X ver 10.4 and lower, but for whatever reason they removed it starting with Leopard...

chown33
Dec 9, 2011, 04:50 PM
Just an FYI, ulimit worked on OS X ver 10.4 and lower, but for whatever reason they removed it starting with Leopard...

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.