How Is a Variable At the Same Address Producing 2 Different Values

How is a variable at the same address producing 2 different values?

This is undefined behavior, you are modifying a const variable so you can have no expectation as to the results. We can see this by going to the draft C++ standard section 7.1.6.1 The cv-qualifiers paragraph 4 which says:

[...]any attempt to modify a const object during its lifetime (3.8) results in undefined behavior.

and even provides an example:

const int* ciq = new const int (3); // initialized as required
int* iq = const_cast<int*>(ciq); // cast required
*iq = 4; // undefined: modifies a const object

In the standard definition of undefined behaviour in section 1.3.24, gives the following possible behaviors:

[...] Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of
a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). [...]

Two different values at the same memory address

Why are there two different datas at the same address?

There aren't. The compiler is allowed to optimize any mention of a const to be as though you had written its compile-time value in there.

Note that the compiler is also allowed to generate code that erases your hard disk when you run it if you do nasty tricks like writing to memory reserved for consts.

Multiple variables have the same address, yet they don't?

The compiler is optimizing away unused variables in your code. This is dead code elimination.

So when you have code like this

int main()
{
int x=5;
int y=4;
int z=8;

printf("%p\n",&z);
}

The compiler can tell that you never use x or y, and therefore can remove that code from the compiled object. This makes your generated code faster and generates smaller object sizes.

Why are the addresses of these two local variables the same?

For starters to output a pointer you need to use the conversion specifier %p instead pf the conversion specifier %d. Otherwise the call of printf invokes undefined behavior.

printf("address of ch is %p\n", ( void * )ch);

Secondly within the function this call of printf does not output the address of the variable ch itself. It outputs the address of the first element of the array pointed to by the pointer expression ch. As the array is not moved within memory then you will get the same value of the address.

To output the address of the function parameter you need to write

void print(char ch[]) {
printf("address of ch is %p\n", ( void * )&ch);
}

Pay attention to that in this call

print(ch);

the array is implicitly converted to pointer to its first element. And the compiler adjusts the function parameter having an array type to pointer to the array element type

void print(char *ch) {

There is a difference between these two calls of printf in main where the source array is declared and within the function

printf("address of ch is %p\n", ( void * )ch);
printf("address of ch is %p\n", ( void * )&ch);

Within the function the first call outputs the address of the first element of the array pointed to by the pointer ch that is the address of the extent of memory occupied by the array.

The second call outputs the address of the local variable ch itself having the type char * (as pointed above).

In main these calls outputs the same value that is the address of the extent of memory occupied by the array. The first call outputs the address of the first element of the array and the second call outputs the address of the array as a whole object. The both values are the initial address of the extent of memory occupied by the array.

Are object variables with the same address always identical in JavaScript?

Two objects cannot occupy the same location, either in our physical universe or in the twisted javascript universe. What you can have are two objects whose values are references to the same memory location.

var x = [1, 2, 3]
var y = x

So, the array [1, 2, 3] might be stored in some memory address, let's say it starts at 00A4. The variable x will be stored in some different place in memory, let's say 0108, and the actual value stored in that location is the number 00A4, that represents the memory location of its referenced value. Now, y will be then store in another totally different memory location, let's say 020F, and its value will also be 00A4, the memory location of its referenced value.

  1. Both x and y are variables whose values are references, that ultimately point to the same address. Reference values that point to the same address are always equal when compared.

  2. Javascript doesn't have any low-level semantics like this to manipulate memory addresses. You could do that maybe by using c procedure calls or something.

C - Local variables have the same address and value

C 2018 6.2.4 2 says:

The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it…

Objects whose identifier is declared inside a function without static or extern have automatic storage duration. The C implementation reserves memory for them automatically and releases the reservation automatically.

The lifetime begins when execution of the block the object is in begins (if it is not a variable length array) or when execution reaches the declaration (if it is a variable length array).

When the body of testA starts executing, memory is reserved for x.

Then you put 56 in x.

Then the function returns, and the block x is in stops executing. So the memory is no longer reserved for it.

Nobody comes along and cleans up that memory. Your mommy does not clean up after you, in the real world or in the computer.

Somebody might come along and use that memory. They ought to initialize it themself, but, if they do not, they might see what you put into that memory.

When testB starts executing, memory is reserved for y. Due to the way these reservations are organized, it is easy for that to be the same memory that was previously reserved for x. Then the value that appears to be in y might be the same value you put in x.

When you turn on optimization, the compiler might redesign the program and eliminate this effect. Or it might not.

pointers with two similar variables in two different functions

If those functions do not call each other, it's possible that calling one, the address will be x, then if the function returns (and the variable does not escape) and the other function is called, the same x address may be reused; but only if the other variable is not live (reachable) anymore. Whether this may be the case is not clear from your question.

Go has automatic memory management. Unless you touch package unsafe, you should not worry about pointers and addresses, it's safe to take and even to return addresses of local variables. The same memory will not be reused if a variable using that memory is still reachable.

One exception is variables that have 0 size. As per the spec, variables having zero size may or may not have the same address. In practice this causes no trouble as you can't change the value of such variables as they cannot store different values, but you can't assume anything about their memory addresses.



Related Topics



Leave a reply



Submit