C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Float the idea: First-class effect annotations and resource contracts for C++

From: Rainer Deyke <rainerd_at_[hidden]>
Date: Tue, 9 Jun 2026 13:44:17 +0200
On 6/8/26 17:28, Arthur O'Dwyer via Std-Proposals wrote:
> [[=stack_usage(256)]] void g() { }
> [[=stack_usage(512)]] void h();
> [[=stack_usage(1024)]] void f() {
> { char buf[1024]; g(); }
> h();
> }
> Does `f` use 1024 bytes of stack, 1024+256 bytes, 1024+512 bytes, or
> 1024+512+256 bytes?

Assuming a halfway competent optimizer, it uses up to somewhat more than
512 bytes. The call to 'g' is inlined and optimized away and the 'buf'
variable is optimized away, so we're left with the call to 'h', which is
declared to take 512 bytes, plus the non-zero stack space used by the
function call itself. It is not possible to know how much stack space a
function actually uses in a portable manner just by looking at it, but
it's possible for the compiler to verify an upper limit to stack usage
in some cases.


-- 
Rainer Deyke - rainerd_at_[hidden]

Received on 2026-06-09 11:44:28