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:58:41 -0800 Organization: None to speak of Lines: 52 Message-ID: <8735mw9hvy.fsf@nosuchdomain.example.com> References: <877dc89i1n.fsf@nosuchdomain.example.com> 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="U2FsdGVkX18n8VFwgXqsKp78l7RIKNob" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:Q9yjR15YXgdZnz858nug6c42Fpg= sha1:TKhWs4NaHf8nutgIy2XwA0vN0hc= Xref: csiph.com comp.lang.c++:82617 Keith Thompson writes: > 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. My apologiess, I didn't notice which newsgroup I was in and gave a C answer. But the C++ standard also discusses the "abstract machine" in section 4.6 [intro.execution], with a very similar meaning. -- 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 */