TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25366 11
Is it possible to force compiler stack allocation?

int main()
{
foo();
// static
// creates two variables, one const and one mutable,
// then prints the section of memory they end up in,
// followed by printing the section name itself.

poo();
// automatic
// does the same thing with a normal local variable
// and prints its section name.

// we also import a disassembler so we can inspect
// how this actually affects the generated code.



// inspector function bytes function address deref jump count
inspectfunction(GrabFunctionBytes(foo), reinterpretcast<uintptrt>(&foo), 1);

inspectfunction(GrabFunctionBytes(poo), reinterpretcast<uintptrt>(&poo), 1);
}

foo output: (statics)

const: .rdata (0x7ff63ff03188)
mutable: .data (0x7ff63ff1c008)

snippet #1

0x7FF63FE7CBB0 | 48 8D 05 51 F4 09 | lea rax, rip+0x9F451 abs: 0x7FF63FF1C008 <--
0x7FF63FE7CBB7 | 48 89 45 08 | mov rbp+0x08, rax
0x7FF63FE7CBBB | 48 8D 05 C6 65 08 | lea rax, rip+0x865C6 abs: 0x7FF63FF03188 <--
0x7FF63FE7CBC2 | 48 89 45 28 | mov rbp+0x28, rax
0x7FF63FE7CBC6 | 8B 05 BC 65 08 00 | mov eax, rip+0x865BC abs: 0x7FF63FF03188 <--

poo output: (automatic)

integer: stack / heap (0xbc06ff454)

snippet #2

0x7FF63FE8B98C | E8 4E 79 FC FF | call -0x386AD
0x7FF63FE8B991 | 90 | nop
0x7FF63FE8B992 | C7 45 04 33 03 00 | mov dword ptr rbp+0x04, 0x333 <--
0x7FF63FE8B999 | 48 8D 45 04 | lea rax, rbp+0x04 <--
0x7FF63FE8B99D | 48 89 45 28 | mov rbp+0x28, rax

What I'm trying to achieve is getting the compiler to prefer stack-based dereferencing over RIP-relative dereferencing not counting branching/jumps and calls ofc.

I know I can control this to some extent by just avoiding static, but it starts getting annoying with things like std::cout and other CRT objects since they naturally end up in sections like .data, .rdata, and .bss.

I've tried a few different methods already. Some worked partly, some didn't really solve the issue. I'm mainly just wondering if there's a cleaner way to push things toward runtime (stack/heap) storage and avoid these RIP-relative accesses, without having to rely on weird hacks or patching the compiler itself.

https://redd.it/1tw3d10
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Sep 29, 2026Boost.Graph 1.95 will be C++17 Dear Boost.Graph community, In two release cycles (Boost re…
  2. Sep 26, 2026Token Sequence Injection & Modern Macros: The Most Game-Changing Compile-Time Feature in C…
  3. Sep 25, 2026myStringStream.str("") Considered Harmful Under C++20 I was recently looking at some (rath…
  4. Sep 20, 2026A clever branch free optimization I'm the developer of memlz which is an extremely fast co…
  5. Sep 15, 2026Inside Boost.PolyCollection https://bannalia.blogspot.com/2026/09/inside-boostpolycollecti…
  6. Sep 11, 2026C++26: Standard Library Hardening Experiments https://www.cppstories.com/2026/hardening-ex…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →