r/ProgrammerHumor • • Oct 20 '21

Meme Stack Overflow in a nutshell

Post image
3.9k Upvotes

117 comments sorted by

View all comments

Show parent comments

5

u/Pockensuppe Oct 21 '21

In your typical GUI framework, decreasing Y will move things upwards, so don't be so quick with assuming direction from a numeric shift. The visualization is already logical, not technical, and rendering it in different directions is isomorphic, therefore no visualization is any more correct than another.

1

u/Bo_Jim Oct 21 '21

A lot of programming is logical rather than technical. I realized this after working with PDP-11's as a repair technician, and then decided to pick up a Macro-11 programming manual. My first thought was "This bears almost no resemblance to how the hardware actually works. This is almost entirely conceptual."

Anyway, in the majority of cases, the stack is an inherent feature of the hardware. When a subroutine is called the pushing of the return address onto the stack is automatic - no extra program instructions are required to make this happen. And while the stack pointer can be set programatically, you can't change the direction it moves when data is pushed onto or popped from the stack. It's decremented after data is pushed, and incremented before data is popped. This behavior is baked into the hardware.

Read either a hardware manual or assembly language manual for a processor and the architecture model they provide will present the stack in exactly this manner - it starts at the initial stack pointer address and grows down through memory. Essentially, the Stack Overflow logo turned upside down. Many operating systems allocate a block of memory for a process, put the code at the bottom, put the heap on top of the code, and put the stack at the top of the block. A stack overflow occurs when the stack grows down far enough to collide with the heap.

1

u/Pockensuppe Oct 21 '21

While PDP-11 was indeed a stack-based CPU, modern popular CPUs are register-based, so stacks are no longer an inherent hardware feature. Instead, runtime stacks are usually defined as part of the OS and/or programming language's ABI and are usually thread-local.

Regardless of the direction in which the stack pointer moves – i.e. whether it is incremented or decremented – this has no intrinsic mapping to the directions up and down.

Moreover, computer science explicitly labels an automaton that uses a stack a pushdown automaton, implying that new elements are appended to the top of the stack and push down existing elements. While you can find all sorts of memory visualization in practical computer science, not once have I seen a stack visualization in theoretical computer science that had new elements not appended to the top. You can argue all you want, but countless computer science papers – I would assume the majority, though I didn't count – visualize a stack oriented exactly the way StackOverflow presents it.

1

u/Bo_Jim Oct 21 '21

That's true of many RISC CPU's. It's not true of CISC CPU's, like Intel X86 architecture. Those have a physical hardware stack pointer, called SP, ESP, or RSP depending on the operating mode and width of the register. You can't select a different general purpose register to perform as the stack pointer, though you could certainly implement a stack in software. Just don't expect the CPU to use it when you execute a CALL instruction. Different processes can have their own stacks, but the stack state (stack segment and stack pointer) has to be saved with the other context data when the context is changed. In other words, there aren't separate physical registers for each process.

Diagrams that show elements being pushed down and popped up are fine for demonstrating the LIFO operation of a stack. But this is, once again, an example of where the concept differs from the way the actual hardware works. Does it make a difference? Not if you never have to troubleshoot an actual stack overflow. It makes a big difference if you're troubleshooting an embedded system program, and you're looking at a logic analyzer trace and don't understand what the registers are doing.

1

u/Pockensuppe Oct 21 '21

I don't get how you can argue so much about hardware details when the simple fact that up and increment being two independent terms that do not necessarily map onto each other voids the whole idea of „wrong orientation of a stack visualization“. It does not make a difference even when working on actual hardware because the actual memory containing the actual stack looks nothing like the visualization anyway, no matter how it is oriented.

1

u/Bo_Jim Oct 22 '21

Well, you pretty made my point. The visualization is wrong.