Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Address of variable needs to be loaded into memory?

I have an issue - with the following code I am trying to find out what is stored at a certain address and how long my static variable is stored at this specific position. (I read that static variables are stored infinitely and was quite surprised - wanted to test if this was true). The code defines a static variable (its address on my system is 0x1000020c0 - This is probably rather random but was continuously the case)

If I now want to find out what integer value is stored at this address I have to first print out the address with $number, which then gives 0x1000020c0. The recasting/reinterpreting of the address (0x1000020c0) gives 100 only! if the address was printed before or if I use &number in the reinterpreting/recasting.

Can someone explain why this is the case?

int static number = 100;  

//  std::cout << number << std::endl; <- prints 100

//  prints 100 and the address 0x1000020c0 in my case
//  std::cout << number << " " << &number << std::endl;

//  this does not work unless &number is printed previously
//  std::cout << "Value is :  " << *reinterpret_cast<int*>(0x1000020c0) << std::endl;

// this does work and show the correct value (100)
std::cout << "Value is :  " << *reinterpret_cast<int*>(&number) << std::endl;
like image 700
Maik Ro Avatar asked Feb 20 '17 07:02

Maik Ro


People also ask

What is memory address of a variable?

The memory address is the location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in this memory address.

Where is the address of a variable stored?

The address of a local variable isn't stored anywhere, unless you have a pointer pointing to the local variable. The local variables themselves are typically stored on the stack on most modern systems.

How variables are stored in the memory?

Variables are usually stored in RAM. This is either on the heap (e.g. all global variables will usually go there) or on the stack (all variables declared within a method/function usually go there). Stack and Heap are both RAM, just different locations. Pointers have different rules.

What is the address of a variable programming?

An address is a non-negative integer. Each time a program is run the variables may or may not be located in same memory locations. Each time you run the program above may or may not result in the same output.


1 Answers

In any given program, the object might, or might not be stored in the address 0x1000020c0. There are no guarantees either way. The address of the object is decided at compile (or possibly at link) time. A change to the program can change the address.

If you never take the address of the local static object, and never modify it, the compiler may optimize the variable away, so that no memory is used. If the object doesn't exist at all, then it definitely doesn't exist at the memory location 0x1000020c0.

If you use the object in a way that requires the object to exist, it will be in some memory location. Taking the address of the object usually triggers such requirement. This is strikingly similar to observer effect in physics.

If you dereference a pointer which does not point to an object (of appropriate type), the behaviour is undefined.


when I print recast/reinterpret the value that is at 0x1000020c0 it prints nothing

As I explained above, the object is not guaranteed to exist at the memory location 0x1000020c0.

even though the object was used since i printed its value via std::cout << number;

Accessing the value of an object doesn't necessarily require the object to exist. The compiler may be able to prove that the value of the object is 100, so it can store that value as a constant and not store static object at all.

Besides, even if the static object did exist, it wouldn't necessarily exist in the address 0x1000020c0, unless you take the address and observe it to be so.


As a consequence: Don't ever cast an arbitrary number to a pointer (unless you work on some embedded platform that has hardcoded memory mappings). Seeing that the address of an object in one program is 0x1000020c0, doesn't make 0x1000020c0 non-arbitrary in another program.

like image 167
eerorika Avatar answered Sep 21 '22 13:09

eerorika