This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
behavior of const_cast in C++ [duplicate]
(3 answers)
Closed 7 years ago.
I'm trying to change value of const variable via its address.
following this code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main(void)
{
uint64_t const x = -1;
uint64_t *b = reinterpret_cast<uint64_t*>(0x28ff10);
cout<< x << endl;
cout<< &x << " " << b << " " << *b << endl;
printf("%p\n", &x);
*b = 10;
cout<< &x << " " << x << " " << b << " " << *b << " " << *(reinterpret_cast<uint64_t*>(0x28ff10)) <<endl;
return 0;
}
Compiled with MinGW 4.8.1:
g++ -g main.cpp && ./a.exe
And this is output:
18446744073709551615
0x28ff10 0x28ff10 18446744073709551615
0028FF10
0x28ff10 18446744073709551615 0x28ff10 10 10
Could anyone explain it ?
EDIT:
Figured out! compile still optimized my variable although I compiled it with -O0. Looked at ASM generated, I saw that printf and cout put directly the value instead of the variable symbol.
So, to make my code do right behavior, I have to declared it with volatile static
I'm trying to change value of const variable via its address.
You've already gone wrong by this point.
const is short for "constant".
You cannot mutate a constant.
Sometimes you can get it to sort of look like you did, but doing so has undefined behaviour. You told your compiler that it can make all sorts of assumptions about x (including optimising it out from your binary entirely!) because you promise that you'll never change it. Then you change it.
No dinner for you tonight, says Mr. Compiler!
Related
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.
This question already has answers here:
Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14?
(1 answer)
Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?
(4 answers)
Closed 6 years ago.
I am confused about the following code:
#include <iostream>
int i = 1;
int main()
{
int i = i;
std::cout << "i: " << i << "\n";
return 0;
}
Output:
i: 0
I had expected running the above code would print 1. Can someone please explain the reason for this strange behavior?
You are initializing i with itself. The both i's in int i = i; are the inner one not the outer one. This is undefined behavior and you may get 0 or anything may happen.
This is the right way if you want to assign the outer i to the inner i.
#include <iostream>
int i = 1;
int main()
{
int i = ::i;
std::cout << "i: " << i << "\n";
return 0;
}
Live Demo
BTW, You should carefully read all the compiler warnings. If you did you could see the problem yourself:
warning 'i' is used uninitialized in this function
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I have this code:
#include <iostream>
using namespace std;
int & squareRef(int );
int main() {
int number1 = 8;
cout << "In main() &number1: " << &number1 << endl;
int & result = squareRef(number1);
// cout << "In main() &result: " << &result << endl;
cout << result << endl;
cout << result << endl;
cout << number1 << endl;
}
int & squareRef(int rNumber) {
cout << "In squareRef(): " << &rNumber << endl;
rNumber *= rNumber;
return rNumber;
}
The program produces the following output:
In main() &number1: 0x28ff08
In squareRef(): 0x28fef0
64
1875681984
8
Can anyone please explain why the two "results" are different , is that suppose to be same isn't ?
You are invoking undefined behaviour by returning a reference to a local variable:
test.cc:19:7: error: reference to local variable ‘rNumber’ returned [-Werror=return-local-addr]
int & squareRef(int rNumber) {
rNumber is copied on the stack for the call. After the call, the value on the stack is undefined, and might well change due to subsequent calls. The reference you return only points to that location on the stack and does not hold the actual value.
In general, when these things happen, it is very helpful to turn on all warnings your compiler can give you. With gcc, the flags -Wall -Wextra -Werror provide you with a lot of helpful warnings, such as these. In general, code should compile without throwing any warnings (except maybe unused variables/parameters in function stubs, although there are macros to explicitly skip over these).
This question already has answers here:
maximum value of int
(7 answers)
Closed 9 years ago.
I thought that unsigned int max possible value is 65535 ( in C++ ) but I created a programm which can use an int which is equal with 100000 for example. Is it safe to use int with values up to 10000000 or the program may crash? In that case is long the only solution?
Thank you for your time!
Use std::numeric_limits<unsigned int>::max() to know for certain what this value is.
Please check the code below for limits:
#include <iostream> // std::cout
#include <limits> // std::numeric_limits
int main () {
std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
return 0;
}
INT_MAX is implementation defined. That means it's up to your compiler vendor to decide, as long as it's no less than 32767, and greater or equal to a short. You can use the climits definitions to discover your implementation's limits:
#include <iostream>
#include <climits>
int main () {
std::cout << INT_MAX << std::endl;
return 0;
}
On my installation of gcc/g++ v4.8.1 targeting x86_64-linux-gnu, this snippet produces:
2147483647
And as has been mentioned in the followup replies to this answer, you may (and probably should) use the more semantically proper (for C++) method:
#include <iostream>
#include <limits>
int main () {
std::cout << std::numeric_limits<int>::max() << std::endl;
return 0;
}
...which ought to produce the same output.
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);