MATLAB: Do I get a Segmentation Fault when I run recursive functions on Linux

MATLAB

Why do I get a Segmentation Fault when I run recursive functions on Linux? Why is the recursion limit lower on Linux machines in MATLAB 6.0 (R12) than it used to be?
The segmentation faults seem to happen when I recurse over 150 levels; but in earlier MATLAB releases these functions used to recurse deeper without any problems.

Best Answer

The newer glibc 2.1 libraries that are used to build MATLAB on Linux handle the stackspace differently than in earlier versions of MATLAB (which were based on libc5). Because of this difference, there is less stack space available for recursions. This is why the default recursion limit on Linux machines was lowered for MATLAB 6.0 (R12):
>> get(0,'RecursionLimit')
ans =
100
You can increase this limit if you want, for example:
>> set(0,'RecursionLimit',200)
However, there's a good chance that you will encounter a segmentation violation caused by running out of stack space.
There are two workarounds that you can try for this:
1. If you hit the recursion limit of 100 in MATLAB, increase it. If you hit the hard limit and cause a segmentation violation, ask your shell to give you more stack space. (if you don't know how to do this or how to use shell commands such as 'limit', you'll need to speak with your system administrator)
2. Upgrade your C library to glibc 2.2. In addition to have fixed some bugs in glibc 2.1, we've found that a machine running glibc 2.2 can support a much larger recursion depth than a machine running glibc 2.1 before blowing the stack (using identical resource limits set via limit/ulimit).