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.
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:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 1 year ago.
I have the following code
# include <iostream>
using namespace std;
void show (int list[], int len) {
for (int i = 0; i < len; i++) {
cout << list[i] << " ";
}
cout << endl;
}
void swap (int* a, int* b) {
cout << *a << ' ' << *b << endl;
int c = *a;
*a = *b;
*b = c;
cout << *a << ' ' << *b << endl;
}
void aFunction (int list[], int len) {
cout << "From aFunction ";
show(list, len);
swap(list[0], list[1]);
cout << "From aFunction ";
show(list, len);
}
int main() {
int list[] = {6, 4};
int len = sizeof(list)/sizeof(int);
aFunction(list, len);
return 0;
}
When I compile and run this I do not get any errors and I receive the output as
From aFunction 6 4
From aFunction 4 6
But when I change the following line in aFunction
swap(list[0], list[1]);
to
swap(&list[0], &list[1]);
it still compiles and give me the following output
From aFunction 6 4
6 4
4 6
From aFunction 4 6
What is going on? My initial thought was swap(list[0], list[1]) is right since arrays decay to pointers when passed to a function so we are essentially passing pointers to swap but then the cout in swap is not printed, which leads me to believe swap(&list[0], &list[1]) is right. If the latter is right, why the compiler does not raise any error for the first and why the cout in swap does not execute? Which one is right? What's going on????? I am compiling this program using g++ weird.cpp on a MacBook Pro. I would be really grateful for an explanation.
You're calling std::swap, not your own swap function.
std::swap takes two reference arguments of any matching type.
(This is why, IMHO, using namespace std; can be a bad idea. I prefer to qualify names with std:: explicitly.)
This question already has answers here:
Assigning two values with an union variable
(6 answers)
Closed 3 years ago.
I was expecting the output to look like: 10 20 10 20
But the output came like this: 10 20 20 20
What is happening behind the code?
#include <iostream>
using namespace std;
typedef struct sdata{
int a;
int b;
union udata{
int a;
int b;
}u;
}Data;
int main()
{
Data s;
s.a = 10;
s.b = 20;
s.u.a = 10;
s.u.b = 20;
cout << s.a << " " << s.b << " " << s.u.a << " " << s.u.b;
return 0;
}
Unlike a struct, a union can only hold one member at a time. Each member starts at the same memory address, so writing to one affects the other.
In this case your union has two fields of type int. So if you set one than the other will contain exactly the same value. So when you set s.u.b to 20 it also sets s.u.a to that value as well.
This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
Can you tell me the difference between the source 1 and 2?
The book says the first one is call by address(pointer) and the second one is call by reference, but i don't exactly get those two sources.
Please explain those sources to me please, thank you in advance.
1.
#include <iostream>
using namespace std;
void absolute(int *a);
void main()
{
int a = -10;
cout << "Value a before calling the main function = " << a << endl;
absolute(&a);
cout << "Value a after calling the main function = " << a << endl;
}
void absolute(int *a)
{
if (*a < 0)
*a = -*a;
}
2.
#include <iostream>
using namespace std;
void absolute(int &a);
void main()
{
int a = -10;
cout << "Value a before calling the main function" << a << endl;
absolute(a);
cout << "Value a after calling the main function" << a << endl;
}
void absolute(int &a)
{
if (a < 0)
a = -a;
}
In terms of what happens at the CPU level, pointers and references are exactly the same. The difference lies in the compiler, it won't let you do a delete on a reference (and there's less typing)
So in your code both functions do the same thing.
This question already has answers here:
Order of execution in operator <<
(4 answers)
Closed 8 years ago.
The code is simple:
int Change(int& a)
{
a = 4;
return a;
}
int main()
{
int a = 10;
cout << Change(a) << a;
}
In C-Free : the output : 4 4
In VS2008 : the output : 4 10
Why? As I have learned, I think 4 4 is right.
Simply put, there is no rule that guarantees that "4 4" is right. Same goes for "4 10".
As others have mentioned in the comments you have a case of undefined behaviour here. Even if this would be defined behaviour, code like this is difficult to understand. So I recommend to do
cout << Change(a);
cout << a;
or
int b = a;
cout << Change(a);
cout << b;
whatever you really want to do.