Disproving Determinism via c++ [duplicate] - c++

This question already has answers here:
Two different values at the same memory address
(7 answers)
C/C++ changing the value of a const
(18 answers)
Closed 5 months ago.
friends.
Recently I experimented a bit a C++ constants.
The code is:
#include <iostream>
int main() {
const int c = 1;
const int* ptr = &c;
int* tmp = const_cast<int*>(ptr);
*tmp = 5;
std::cout << &c << " " << ptr << " " << tmp << "\n";
std::cout << c << " " << *ptr << " " << *tmp;
}
I have investigated assembly code at godbolt: https://godbolt.org/z/7e3o7bWrs
The assembly code seems like doing what I wrote, exactly moving address of c into tmp variable and changes variable at this address.
Can you please tell me, why there is could be two different values at the same addresses?
Thank you.

Related

strange output after const_cast [duplicate]

This question already has answers here:
Getting the same value of the const variable even after changing it through const_cast
(5 answers)
How to use const_cast?
(2 answers)
Closed 2 years ago.
here is my code:
#include <iostream>
using namespace std;
int main()
{
const int a = 10;
cout << a << endl;
int *b = const_cast<int *>(&a);
cout << *b << endl;
(*b)++;
cout << *b << endl;
cout << a << endl;
return 0;
}
output on ubuntu20, g++ 9.3.0
-----
10
10
11
10
the output puzzles me. pointer b points to int a. I have cast it to int *. why the output for a is not changed and the b has changed. what's the mechanism of const_cast working. I am a new learner of cpp, I used java.

Pointer address and address of variables to which pointer points [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 2 years ago.
I am a newbie in c++ and my question may seem basic, but your answer could help me and help others.
I created to char pointer myPointer1 und myPointer2 so
const char *myPointer1 = "Hallo";
const char* myPointer2 = myPointer;
I thought that pointer stored the address of the variables they point to. In this case we have just one variable "Hallo" and both pointers should then points to the same address.
but when i print :
cout << &myPointer1 << endl << endl;
cout << &myPointer2 << endl << endl;
the results are two different adresses:
009EFC00
009EFBE8
Could anyone help?
You are printing the address of the pointer, not the address that the pointer points to.
std::cout << myPointer << std::endl;
This would print the address the pointer points to.
Since a char* is treated as a string when passed to std::cout it will print Hallo.
If you want to print the address itself you can achieve that by casting it to a const void* and printing that.
#include <iostream>
int main() {
const char *myPointer1 = "Hallo";
const char* myPointer2 = myPointer1;
std::cout << static_cast<const void*>(myPointer1) << std::endl;
std::cout << static_cast<const void*>(myPointer2) << std::endl;
}

Elements in array keep changing unintentionally [duplicate]

This question already has answers here:
Returning local data from functions in C and C++ via pointer
(13 answers)
Closed 5 years ago.
So I have two arrays which hold players x, y and z co-ordinates, however each time I call them they change their values the first time they are printed to the console they display the correct result however subsequent prints to the screen yield very small numbers,
HisPosition = GetPlayerPosition(aPlayer);
std::cout << "TheirPos=" << std::dec << HisPosition[0] << std::endl;
std::cout << "TheirPos1=" << std::dec << HisPosition[0] << std::endl;
if(!isnan(HisPosition[0])){
std::cout << "TheirPos2=" << std::dec << HisPosition[0] << std::endl;
Example console output:
TheirPos=440
TheirPos1=1.7118e-037
TheirPos2=1.7118e-037
They are defined like so:
float* HisPosition;
And GetPlayerPosition:
float* GetPlayerPosition(DWORD dwBase)
{
float result [3];
DWORD aXPos = dwBase + 0x134;
DWORD aYPos = dwBase + 0x138;
DWORD aZPos = dwBase + 0x13C;
result[0] = Read<float>(aXPos);
result[1] = Read<float>(aYPos);
result[2] = Read<float>(aZPos);
return result;
}
aPlayer stands for the address in memory of the player I am trying to fetch the offsets are correct as on the first console print the position is correct.
I'm new to using arrays in C++ and would appreciate any guidance.
return result; returns a dangling pointer to local, stack-allocated array that gets vanished when goes out of scope.

const_cast - Removing the constness of a simple integer. Same memorly location has different values [duplicate]

This question already has answers here:
Strange behavior of const_cast [duplicate]
(4 answers)
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 8 years ago.
I am using const_cast to remove the constness of an integer.
#include<iostream>
int main(){
const int pi = 3;
int & pie = const_cast<int &>(pi);
pie = 4;
std::cout<<pi << " " << &pi << " " << pie << " " << &pie << std::endl;
};
Output:
root#ubuntu-OptiPlex-380:~/cpp# ./a.out
3 0x7fffdbc66f1c 4 0x7fffdbc66f1c
Can anyone kindly explain how the same memory location can hold different values?

C++ setting char pointer to null [duplicate]

This question already has answers here:
unable to print char* pointing to 0
(4 answers)
Closed 9 years ago.
In Visual studio 2012, I was messing around with pointers, and I realized that this program kept crashing:
#include <iostream>
using std::cout;
using std::endl;
int main ()
{
const char* pointer = nullptr;
cout << "This is the value of pointer " << pointer << "." << endl;
return 0;
}
My intent was the set a pointer to nullptr, and then print the address. Even though the program compiles, it crashes during runtime. Can someone explain what is going on?
Also, what's the difference between pointer and *pointer?
You are using a const char* which, when used in std::cout's operator <<, is interpreted as a string.
Cast it to void* to see the pointer's value (the address it contains):
cout << "This the value of pointer " << (void*)pointer << "." << endl;
Or if you want to be pedantic:
cout << "This the value of pointer " << static_cast<void*>(pointer) << "." << endl;
LIVE EXAMPLE
Although you can do cout << "Pointer is " << (void*)pointer << ".\n"; as already been suggested I feel that in this case the "C way" of doing it is prettier (no casting) and more readable: printf("Pointer is %p\n",pointer);