Cast void pointer to int [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 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;

Related

Is it safe and portable to cast pointer to any type to char pointer for arithmetic? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Considering the following snippet, and for both C and C++ languages, is:
int t[2] = { 0, 1 };
*( (int *)( (char *)t + sizeof(int) ) ) = 2;
always equivalent to:
int t[2] = { 0, 1 };
t[1] = 2;
?
It might sound weird but I imagined some reasons why it could be false on some platforms:
char pointer increment could go in a reverse way than int pointer, unless it is not authorized by the standard (in this case I would like to find where the standards states so)
the cast from int * to char * could be unsafe because there is not guaranty that their size are the same (and in this case, could it work with an intermediate cast to void *?), and because the standard does not guaranty that casting to char * and then to a pointer to any type is safe
Is it safe and portable to cast pointer to any type to char pointer for arithmetic?
Any type --> No.
(Non-null) function pointers cast to char * is UB and then math applied is UB.
A char * may even be too small to encode a function pointer.

Returning pointer to an array of arrays created using new [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 3 years ago.
Improve this question
I have created an array of arrays using char (*H)[N] = new char[M][N];
I want to return this pointer to my main function. My question is what should the function return type be in this case? Am I allowed to have a return type as a pointer to an array of arrays.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes, this would be the syntax for your particular case:
char (*get_array())[N] {
return H;
}
But you really should consider using either std::unique_ptr<char[N]> or std::array<std::array<char, N>, M>.
Am I allowed to have a return type as a pointer to an array of arrays.
Yes. One way to do that would be to define a type alias and use it as the return type.
using MyArrayPointer = char (*)[N];
MyArrayPointer foo()
{
auto ptr = new char[M][N];
return ptr;
}

2 value in one variable (const and const_cast) 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
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.

C++ Pointer type cast from class name [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
How to cast a Pointer in C++ from class name in String?
Psuedocode:
int * ptr = something;
myStruct ptrstruct = (ClassFromString("myStruct") ptr);
// The class/struct name is passed in as String
Thank you
I'm not a c++ guru but I have two ideas that may help with brainstorming:
May the use of the registry pattern as described here would be of help: Instantiate class from name?
Secondly, following the registry pattern idea you could crate a function for casting e.g. MyClass something = registry.cast("MyClass", ptr);
I am not sure but this must work
Only Void pointer or boost can help, if it happens
thing * p = something; // pointer to object
void * pv = p; // pointer to void
thing * p2 = static_cast<thing *>(pv); // pointer to the same object
Maybe same kind of situation is while returning values from Threads
Overall reflection is not possible in c++.
Its just brainstroming.

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.