Relative Content

Tag Archive for variables

Do there exist programming languages where a variable can truly know its own name?

In PHP and Python one can iterate over the local variables and, if there is only once choice where the value matches, you could say that you know what the variable’s name is, but this does not always work. Machine code does not have variable names. C compiles to assembly and does not have any native reflection capabilities, so it would not know it’s name. (Edit: per Anton’s answer the pre-processor can know the variable’s name).

Stack and heap – dynamic allocation question

Sources usually mention that dynamically created variables are allocated on the heap, while functions’ variables on the stack. Also the ones on the stack cease to exist automatically when e.g. the function which contains the variable exits. If I understand correctly, data fields of class are on the heap.
I do not understand, what is meant by ‘dynamic’? As I see it, when the code is running, anything being created is created dynamically on the fly, be it function variables or objects with variables inside them. I would be glad for simple explanation. Thanks