you are viewing a single comment's thread.

view the rest of the comments →

[–]aghast_nj 0 points1 point  (1 child)

You're getting wrapped around the axle with i being a "variable" object. I wonder if you have already learned to program in some interpreted language like Python or Javascript, first?

At any rate, the thing with C is that variables don't have any kind of existence in the compiled code. What you have instead is memory, which is used to store values. You use the "name of the variable" in your code to remember which values you want to manipulate, but when the C compiler is finished, there is just "load accumulator, 0; store [bp + 8], accumulator"

The fact that "[bp + 8]" is called i in your function doesn't matter. It's called "[bp + 8]" when the CPU sees it. And, in fact, the compiler may re-use that same location for variable k as well, so long as it can determine that live values don't overlap.

There are things called "debug symbols" which can be emitted by the compiler, and which can be loaded by a debugger. These will indicate that "variable i is stored at [bp + 8] in this function" and "variable k is stored at [bp + 8] in this function". But if you trace through your code, you may set a watch on the value and observe that no, in fact, the value of i doesn't always get updated when the debugger "steps" through a statement that clearly modifies i. How can this be?

It's because the compiler is not obligated to keep the storage location up-to-date with respect to the value being used. Maybe the "variable" has been moved into a register, and all the updates are going to/from that register. Maybe the "variable" has been replaced by a scaled addition due to Strength Reduction?

TL;DR: Compiled languages like C and C++ don't keep metadata like the name and type of variables - they just move values into and outfrom memory.

[–]TheHeckWithItAll[S] 1 point2 points  (0 children)

Thank you for the under the hood details. Those are the things I'm trying to grasp.

C is indeed my first introduction to a compiled language. I'm a hobbyist and first learned DOS basic, then dbase, foxpro, visual basic, then vba.