Uninitialized pointer, points to zero [duplicate] - c++

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.

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)

Bug in gcc / g++? Initialization using member initialization list for an object which does not exist yet [duplicate]

This question already has answers here:
What's the behavior of an uninitialized variable used as its own initializer?
(3 answers)
Closed 4 years ago.
Consider some code:
#include <iostream>
int main()
{
using std::cout;
int a=3;
cout << "a="<<a<<"\n";
{
int a=a;
cout << "new a = " << a << "\n";
a=5;
cout << "a = " << a << "\n";
}
cout << "old a = " << a << "\n";
}
I'd expect it to print
a=3
new a = 3
changed a = 5
old a = 3
But what I get actually appears to say new a = 0 in the second line. I thought that it would work like initialization list in a class' constructor, where one can write like
C::C(int a) : a(a) {}
But for some reason this is different. First, removing the outer code completely doesn't result in a compilation error. So I assume that int a=a; is valid. Turning on all the compiler warnings leads to this:
test.cpp: In function ‘int main()’:
test.cpp:10:15: warning: ‘a’ is used uninitialized in this function
int a=a;
So my question now: why is this syntax valid at all? Why doesn't the compiler say something like "undefined variable a"?
It's syntactically valid, since the variable's point of declaration comes before its initialiser, and the name is available anywhere after that point. This allows less dodgy initialisations like
void *p = &p;
which legitimately uses the name (but not the value) of the variable being initialised.
It's behaviourally invalid, since using the value of an uninitialised object gives undefined behaviour. That's not an error that requires diagnosis (since, in general, it can be difficult or impossible to analyse the program flow to see whether an object has been initialised), but as you note, many compilers will give a warning for straightforward cases like this.

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.

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

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.