|
|
Subscribe / Log in / New account

resource usage concerns

resource usage concerns

Posted Mar 20, 2025 22:39 UTC (Thu) by wahern (subscriber, #37304)
In reply to: resource usage concerns by tialaramex
Parent article: Oxidizing Ubuntu: adopting Rust utilities by default

The stack does shrink. Example program:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

__attribute__((noinline))
static void showfp(unsigned n, intptr_t otop) {
	intptr_t top = (intptr_t)__builtin_stack_address();
	printf("n:%u off:%td\n", n, (otop > top)? otop - top : top - otop);
}

int main(void) {
	unsigned n = 0;
	intptr_t top = (intptr_t)__builtin_stack_address();
	while (1 == scanf("%u", &n)) {
		char buf[n];
		memset_s(buf, n, 0, n);
		showfp(n, top);
	}
	return 0;
}

For `echo 200 100 5 | ./a.out` I get:

n:200 off:272
n:100 off:176
n:5 off:80

As the size of successive stack allocations decrease, so does the frame size.


to post comments

resource usage concerns

Posted Mar 21, 2025 13:39 UTC (Fri) by tialaramex (subscriber, #21167) [Link] (2 responses)

Ah, a VLA, yes it makes sense that the VLA has to actually allocate. I hadn't considered VLAs in what I wrote.

I assume, since your example is a VLA that if you write a conventional C89 array or any other type it is not in fact creating and destroying the allocation.

resource usage concerns

Posted Mar 21, 2025 22:42 UTC (Fri) by wahern (subscriber, #37304) [Link] (1 responses)

Yes, tweaking that example program it seems a statically sized array, even if declared within a runtime conditional block at the end of the routine (after the VLA loop), is indeed allocated at the start. Not surprising (I didn't disbelieve in that respect), but now I wonder if, in the days before GCC and clang implemented stack probing, that behavior posed a security issue for functions that attempted to conditionally use a [non-VLA] stack allocation based on its own stack size check. Perhaps still something to keep in mind for some other compilers, both C and non-C.

For posterity: I've been using gcc version 14.2.0 (MacPorts gcc14 14.2.0_3+stdlib_flag) on an ARM M1 with these test cases. (__builtin_stack_address was too convenient, but not supported by the installed Apple clang toolchain, though it seems it is supported by the latest upstream clang release.)

resource usage concerns

Posted Mar 21, 2025 23:29 UTC (Fri) by tialaramex (subscriber, #21167) [Link]

Re-reading your original comment I observe that it is pretty emphatic about VLAs and yet I somehow ended up thinking only about the ordinary cases (the VLAs probably existed by the time I started getting paid to write C but I think I was still writing more or less C89 well into this century).

So that's on me. It did cause me to go find out what the current status is of (formally supported rather than as a hack) VLA-like Rust objects (ie a runtime sized object lives on the stack) and seems like they're not close.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds