Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.straub-nv.de!dedekind.zen.co.uk!zen.net.uk!hamilton.zen.co.uk!prichard.zen.co.uk.POSTED!not-for-mail Newsgroups: comp.unix.programmer From: Geoff Clare Subject: Re: Avoiding recursive stack overflow in C on Unix/Linux? References: User-Agent: XPN/1.2.6 (Street Spirit ; Linux) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Date: Thu, 5 May 2011 13:40:01 +0100 Message-ID: <1eia98-ftk.ln1@leafnode-msgid.gclare.org.uk> Lines: 33 Organization: Zen Internet NNTP-Posting-Host: e3d53073.news.zen.co.uk X-Trace: DXC=KVPU04332TPQfPBdY>`=OX0g@SS;SF6nWRiiCXJE[K>W>16WA4J_G?RJmKn^0gjVoQ\Gn1>W@EXmV X-Complaints-To: abuse@zen.co.uk Xref: x330-a1.tempe.blueboxinc.net comp.unix.programmer:312 Xavier Roche wrote: > On 05/05/2011 11:37 AM, boltar2003@boltar.world wrote: >> If a program recurses too deeply there's always a chance that it could >> run out of stack space and die with a SIGSEGV. Is there any API that can >> tell you how much stack space is left or some other method to prevent this >> in C/C++? I realise I could catch the signal but thats a pretty damn ugly >> way to do it. > > There is no portable way to find the stack bottom/size ; I am not even > sure what getrlimit(RLIMIT_STACK) returns exactly (current thread stack > size ? main thread stack size ? default stack size ?) Here is what the POSIX/SUS standard says about RLIMIT_STACK: This is the maximum size of the initial thread's stack, in bytes. The implementation does not automatically grow the stack beyond this limit. If this limit is exceeded, SIGSEGV shall be generated for the thread. If the thread is blocking SIGSEGV, or the process is ignoring or catching SIGSEGV and has not made arrangements to use an alternate stack, the disposition of SIGSEGV shall be set to SIG_DFL before it is generated. So it is just for the initial thread (which I assume is what you meant by "main thread"). Created threads each have their own stack size which can be set in the thread attributes. If the OP is contemplating catching the SIGSEGV he should take note of the part about using an alternate stack (for the catching function). -- Geoff Clare