2 value in one variable (const and const_cast) c++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?

The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.

Related

Cast void pointer to int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
With a VC++ project I have a line:
FXint realIndex = (int)m_cmbDevice->getItemData(indx);
This reads a void pointer value from combo box to an int value. In VC++ it compiles and works well.
Now I have to port this to Linux and there I get a compile error
cpp:514:20: error:
cast from pointer to smaller type 'int' loses information
FXint realIndex = (int)m_cmbDevice->getItemData(indx);
Now I use
std::size_t x = reinterpret_cast<std::size_t>(m_cmbDevice->getItemData(indx));
FXint realIndex = x;
My question is now, is this the right way to go?
reinterpret_cast is dangerous and is typically used only to interpret a pointer as another kind of pointer. You should use static_cast instead
size_t is not necessarily big enough to store a pointer. There are already intptr_t and uintptr_t which are signed and unsigned integer types that are capable of holding a pointer
FXint realIndex = x; still raises a warning if x is wider than FXint, so another cast is necessary to turn off all related warnings
So you need to do this
auto x = reinterpret_cast<uintptr_t>(m_cmbDevice->getItemData(indx));
auto realIndex = (FXint)x;

Variable declare & Assign value in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to declare a variable without assigning value in C++?
int a; int a=15; Which one will be Correct?
If i assign value to a variable 3 times or more which one will count at the end in C++??
int a=15;
int a=10;
int a=5;
Which value will be execute for a at the end?
int a; // declared but not assigned
a = 1; // assigning a value
a = 2; // assigning a different value
a = 3; // assigning another value
std::cout << a << "\n"; // will print 3 since only the last assignment matters
int x; declares a variable x without assigning a value.
If you assign to a variable three times then which ever assignment executed last will be the variables final value. Very important idea, C++ statements execute in a specific order, and which order they execute in is essential to understanding what a program does.

c++ nested structure initialization and accessing the members [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
struct Point
{
double x,y;
};
Point p;
struct Disk
{
Point center;
int radius;
};
Disk d;
int main()
{
d.center.x=1.2;
cout<<p.x;
}
Could someone please explain me the output of this code?
why am I not getting the value of x as 1.2 and 0 instead?
Let's go through your code line by line.
First, you created a Point called p. So p is sitting in the memory somewhere:
Memory: p:[x, y]
Then, you created a Disk called d, which stores it's own Point object inside it.
Memory: p:[x, y] d:[center:[x, y], radius]
These are completely separate objects. When you modify the Point stored in d with d.center.x=1.2, it does not affect p at all.
Therefore, p is uninitialized, and reading the value of uninitialized variables causes undefined behavior, meaning anything can happen, in this case usually getting a random value.

Modify a char* in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to modify a c string in C++.
void modify(char* s)
{
s[0] = 'a';
}
If I do this, there will be some undefined behavior and can't run.
Let's assume s[0] is valid. I know char* s is immutable. Is there any possibility that I can modify the s[0] in place, which means, without creating a new string. Do the modification on the original string.
I think you might be misunderstanding some other answers you have seen on the web.
It is only undefined behavior to modify a string constant, not any char*.
As long as you strdup the constant string into a non-constant string, you can make whatever changes you want with it, because it is now in a mutable region of memory.
#include <stdio.h>
#include <string.h>
void modString(char* changeMe) {
changeMe[0] = 'g';
}
int main(){
char* foo = strdup("food");
puts(foo);
modString(foo);
puts(foo);
free(foo);
}

Why do I need to const_cast when releasing memory for a const data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
While releasing memory of a const data, why do I need to const_cast it. What will be the result If I ignore it.
You don't. This is completely valid, standard conform, code:
int const * const a = new int(42);
delete a;
It sounds like you are using std::free from <cstdlib> to do this instead, which signature is
void free(void *);
In this case the implicit conversion from int const * const to void * fails because you can only implicit const-cast, not cast const away. You want the first version in C++ anyway.