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.

Post History

81%
+7 −0
Q&A Behavior of Pointer Arithmetic on the Stack

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 al...

posted 3y ago by Olin Lathrop‭  ·  edited 3y ago by Olin Lathrop‭

Answer
#2: Post edited by user avatar Olin Lathrop‭ · 2021-12-14T18:29:09Z (about 3 years ago)
  • <blockquote>I'm able to access a, since b is below a on the stack.</blockquote>
  • <b>No, it's not!</b>
  • 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 10h 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.
  • <blockquote>I'm able to access a, since b is below a on the stack.</blockquote>
  • <b>No, it's not!</b>
  • 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.
#1: Initial revision by user avatar Olin Lathrop‭ · 2021-12-14T14:23:48Z (about 3 years ago)
<blockquote>I'm able to access a, since b is below a on the stack.</blockquote>

<b>No, it's not!</b>

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 10h 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.