P
Phlip
Noah said:The word illegal has a well defined definition in the English language,
let us look to it:
You snipped the part of my post where I flamed you for what I knew you'd
reply.
Please calm down.
Noah said:The word illegal has a well defined definition in the English language,
let us look to it:
Phlip said:You snipped the part of my post where I flamed you for what I knew you'd
reply.
What??!!
Please calm down.
Consider the code
class A {
private:
int a;
};
int main(void) {
A x;
int* ptr = (int*)&x;
*x = 10;
return 0;
}
I understand that I can not change the private attributes of a class,
but what does standard have to say about the code above?
Tomás said:[..]
Then someone posed the argument that you can cast a pointer to an
"int" and back to the original pointer type and that it will work
perfectly.
Here's some sample code:
std::string* p = new std::string("Hello");
int num = (int)p;
std::string* k = (std::string*)num;
cout << *k;
delete p;
The Standard gives no guarantee whatsoever that the above code will
work. The Standard says that you can cast a pointer value to an
integral type, and then back to the original pointer type, but the
final value will only be legitimate if the integral type had enough
bits to store the pointer value. Nowhere in the Standard are we given
any guarantee that any of the integral types are large enough to hold
any of the pointer values; so even if you use an "unsigned long", you
still can't be sure it will work. It's not 100% portable, and so, it
has no place in the realms of Standard C++.
that the Standard gives no guarantee whatsoever that the value in "p_int" is
valid -- it may be corrupt. Even if we cast it back to an "std::string*",
we've no guarantee that it points to the object called "object".
Victor said:Tomás said:[..]
Then someone posed the argument that you can cast a pointer to an
"int" and back to the original pointer type and that it will work
perfectly.
As an example, MS Visual C++ for Win64 has its 'unsigned long' only
half the size of an object pointer, for example. However, it does have
the __int64 type (implementation-specific) that can hold a value of
an object pointer.
Noah said:footnote 65 states: "The types may have different cv-qualifiers,
subject to the overall restriction that a reinterpret_cast cannot cast
away constness."
Phlip said:...or volatility.
I thought it also cannot cast-in constness (or volatility).
Now, if such an operation had no place in std c++ then I suppose you
should tell the people that wrote the standard as they explicitly layed
out what such an operation's effects would be.
MS Windows programming newsgroup, I'd conceed that there's nothing wrong
with storing an object's address in an integral type.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.