c++, How do two pointers to same location hold different values [duplicate] - c++

This question already has answers here:
Two different values at the same memory address
(7 answers)
Closed 5 years ago.
Someone showed me the following code snippet and asked what it should output
#include <iostream>
using namespace std;
int main() {
const int value = 10;
int* p = (int*)&value;
*p = 20;
cout << value << " " << *p << endl
<< &value << " " << p << endl;
return 0;
}
As you can see, there's a constant value which is 10, and there's a pointer p which points to the address of value, then the address pointed gets a different value.
I expected the program to print 20 20 but it actually prints 10 20.
It also shows that the two valuables have the same address. Can someone explain what's going on behind the scenes here?

Undefined behavior and an optimizing compiler. The compiler knows from value's declaration that value's value will never change in a well-formed program, so it optimizes out the bits where value's value would be checked and just takes the value it knows value has.
As for the addresses, you never take the address of p, and that p is the same as &value is not surprising because you assigned it that way a few lines earlier.

Related

Garbage value problem , I want to get a garbage value but I am getting zero...Why is it so [duplicate]

This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 4 years ago.
#include <iostream>
int main() {
int* pt;
std::cout << pt << std::endl;
if(pt)
std::cout << "true" << std::endl;
else
std::cout << "false" << std::endl;
return 0;
}
This code fragment evaulate to false. What is more it prints 0 in
std::cout << pt << std::endl;
I thought that uninitialized pointers points to some random address, which may contain garbage value. In this case it points to 0 value like i would assign it to nullptr. Could someone clarify this for me?
This code invokes Undefined Behavior (UB), since the pointer is used uninitialized.
The compiler should emit a warning, when a warning flag is used, like -Wall for example:
warning: 'pt' is used uninitialized in this function [-Wuninitialized]
std::cout << pt << std::endl;
^~
It just happens, that at this run, on your system, it had the behavior of a null pointer. That means that the garbage value the pointer takes, happens to be 0. However, notice that, kernel zeroes appear relatively often.

Value of const changed in the memory but not on the output [duplicate]

This question already has answers here:
Modifying a const through a non-const pointer
(6 answers)
Closed 2 years ago.
I'm trying to write a program that changes the value of a const variable. I know this shouldn't be done this in the first place and I'm doing this just to know how this happen.
I've already read other questions about this but I'm not trying to know how to do it but why it isn't happening in my program. This is the code I wrote:
int main()
{
const int a = 5;
int *pointer_a = const_cast<int*>(&a);
*pointer_a = 6;
// Address of a
std::cout << &a << std::endl;
// Prints 5 while memory says it is 6
std::cout << a << std::endl;
// Address that pointer points too
std::cout << pointer_a << std::endl;
// Prints 6
std::cout << *pointer_a << std::endl;
}
What I noticed while debugging this program is that in fact the value at the memory address of a gets updated. It does change the value of a from 5 to 6.
The IDE (Visual Studio) also shows the value of a to be 6 but when printing it to the console, it prints 5. Why does this happen when the current value on the memory is 6?.
It is undefined behaviour to modify a value which is initially declared as const.
https://en.cppreference.com/w/cpp/language/const_cast:
const_cast makes it possible to form a reference or pointer to
non-const type that is actually referring to a const object. Modifying
a const object through a non-const access path results in undefined
behavior.
try turning off compiler optimization flags (Project Properties, C/C++, Optimization)

Uninitialized pointer, points to zero [duplicate]

This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 4 years ago.
#include <iostream>
int main() {
int* pt;
std::cout << pt << std::endl;
if(pt)
std::cout << "true" << std::endl;
else
std::cout << "false" << std::endl;
return 0;
}
This code fragment evaulate to false. What is more it prints 0 in
std::cout << pt << std::endl;
I thought that uninitialized pointers points to some random address, which may contain garbage value. In this case it points to 0 value like i would assign it to nullptr. Could someone clarify this for me?
This code invokes Undefined Behavior (UB), since the pointer is used uninitialized.
The compiler should emit a warning, when a warning flag is used, like -Wall for example:
warning: 'pt' is used uninitialized in this function [-Wuninitialized]
std::cout << pt << std::endl;
^~
It just happens, that at this run, on your system, it had the behavior of a null pointer. That means that the garbage value the pointer takes, happens to be 0. However, notice that, kernel zeroes appear relatively often.

memory content not erased after deleting my pointer (on a simple example) [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 7 years ago.
Here is a simple of example of a question I have:
I create a pointer of integer (value 5), I print the pointer (hence the address) of the memory case it points to, as well as its content (5).
Then I delete that pointer, which is supposed to delete the content in the memory case that has the address of the pointer.
But then when I print a and its content, as expected the address still exists. Nevertheless the content remains the same (5) as if a was not deleted...
could you explain me? :-)
#include <iostream>
int main()
{
int * a = new int(5);
std::cout<< a << std::endl;
std::cout<< "--------" << std::endl;
delete a;
std::cout<< a << std::endl;
std::cout<< *a << std::endl;
// end of my main
return 0;
}
result:
0x7fff28403a90
--------
0x7fff28403a90
5
It may or may not print the same value after delete operation. So simply what you are observing is an undefined behavior.

Value of pointer to stack integer (becomes NULL)? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
Given the following code snippet:
#include <iostream>
using namespace std;
int *pPointer;
int func()
{
int num;
num = 25;
pPointer = &num;
}
int main()
{
func();
cout << *pPointer << endl;
cout << *pPointer << endl;
return 0;
}
Can anyone tell me if I duplicate the following line:
cout << *pPointer << endl;
cout << *pPointer << endl;
Why I receive 25 (as expected) but then the next value is 0 (NULL)?
Wouldn't the value of int=25 still remain on the stack? What is it about cout that changes the value of pPointer? Am I missing something about my understanding about scope and the stack? (I'm a Java guy, so this was surprising).
This is because pPointer is pointing to a local variable in func(). This area of memory is only valid inside of the func() method. After func() exits, the memory that pPointer points to is free to be re-used, and appears to be used by cout.
If you want to persist this memory, you should either save it by value, or allocate the memory dynamically pPointer = new int(num)
num does not exist once the function func() terminates.
You take its address when it exists, then attempt to print the value at an address that is no longer valid.
Anything can happen.
As it is undefined behavior, there is no num outside of func scope.
cout << *pPointer << endl;
here *pPointer is pointing to a local variable of some other function which already returned so the address to which it is pointing is out of scope so the behaviour is undefined. it may print the right value assigned to num or may not.
Why I receive 25 (as expected) but then the next value is 0 (NULL)?
What you mention as expected is actually not expected!
num becomes out-of-scope as soon as func() returns. Although pPointer is valid (as it is global), *pPointer is not, because it is pointing to a out-of-scope memory.