Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Behavior of Pointer Arithmetic on the Stack

Parent

Behavior of Pointer Arithmetic on the Stack

+9
−0

Consider the following code:

#include <stdio.h>

int main() {
    int a = 5;
    int b;
    ++*(&b + 1);
    printf("%d\n", a);
    return 0;
}

The output is as expected:

6

By creating and incrementing a pointer to b, I'm able to access a, since b is below a on the stack. Is this behavior guaranteed by the C language, or is this undefined/unspecified behavior? If UB, what does the standard have to say that disallows this? For example, does C guarantee that the stack grows downwards, or that arithmetic with pointers into the stack is valid?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (1 comment)
Post
+7
−0
I'm able to access a, since b is below a on the stack.

No, it's not!

You have no guarantee in what order the compiler allocates temporary variables on the stack, and even whether it does so at all. You don't even have a guarantee which way (towards high or low addresses) the stack grows. Different compilers on the same machine might do it differently. I've seen this on a PIC 18, for example.

On machines with a lot of registers, both variables might be kept solely in registers when there is little other demand for those registers.

The worst scenario is that whatever is one address past "b" isn't a general memory location, and reading it has side-effects. That's unlikely in this simplified example, but nothing in the standard rules it out. For example, if "b" happened to be allocated to the last register, and that register was mapped into data memory, then something completely unexpected could be at the next address.

Consider an architecture like a Microchip dsPIC. This machine has 16 16-bit registers that are also mapped to addresses 00h to 1Fh in data memory. If the compiler happened to allocate "b" to W14, then the next word is W15, which is the stack pointer. Reading it wouldn't have any side effects in this case, but writing it surely would. On a different architecture, you might end up reading the UART input data register, thereby clearing the last received data. That's unlikely, but you don't know it's not the case without specific knowledge of the machine and the compiler.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Even normal stack layouts may lead to such effects (1 comment)
Worst case - undefined behavior (1 comment)
Worst case - undefined behavior
Lundin‭ wrote almost 3 years ago · edited almost 3 years ago

An even worse case: since this is undefined behavior, the compiler is by no means required to generate deterministic code. Check out this example with clang for x86_64 https://godbolt.org/z/EbTGWMx93. The calling convention here is that the esi register must be set with the value to print. Since I'm compiling with full optimizations, nothing actually gets allocated on the stack. Instead in good_code, we can see that from the optimized disassembly that the compiler just loads 1 directly into the register. In bad_code, not only the stack allocation, but the whole register setup was simply omitted from the machine code. The compiler isn't even attempting to access the stack or any memory beyond the requested address, it just prints some random garbage that happened to be inside some register. This is exactly why one should never rely on undefined behavior to do anything in particular.