Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Has "stack overflow" specified behavior? Date: Mon, 13 Dec 2021 15:55:16 -0800 Organization: None to speak of Lines: 47 Message-ID: <877dc89i1n.fsf@nosuchdomain.example.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="abb7162027a59d3701de80734b2d80ee"; logging-data="7453"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/B2HOZAXt0S32y1WF95tQv" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:mjzz/dNNXAHa+U1uVAxd8Lv61xo= sha1:YZEM9WxnY3Jm0L2zDahc+RpBqlY= Xref: csiph.com comp.lang.c++:82616 wij writes: > void t() { > int a; > ++a; > t(); > }; > > int main() > { > t(); > } > > --- > Has "stack overflow" specified behavior? The standard does not specify what happens when a resource limit is exceeded. In my opinion that matches the standard's definition of "undefined behavior" (behavior that is not defined). Is the variable `a` intended to track the depth of the recursion? It doesn't. A new instance of `a` is created every time t is called. You didn't initialize `a`, but if you initialized it to 0 then `++a` would simply set it to 1. If you made `a` static, it would count the depth of the recursion -- and you'd have undefined behavior after the value of `a` reaches INT_MAX. With `a` defined as you did here, there's a distinct instance of `a` for each call to t, and each instance has its own distinct address, a value of type int*. There can be at most 2**(CHAR_BIT * sizeof (int*)) distinct int* values. If you never refer to the value or address of `a`, an optimizing compiler is likely to eliminate it and change the recursive call to a loop, but that doesn't apply in the abstract machine. See N1570 5.1.2.3 "Program execution": The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant. In the abstract machine, each instance of `a` occupies sizeof (int) bytes and has a unique address of type int*. After optimization, `a` might not exist. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */