- We are casting a struct to a int**.
- We are modifying the value of a constant private member.
- We are modifying the value of a constant variable on the stack.
Tested with Visual Studio 2012
#include <iostream>
struct PtrHolder
{
PtrHolder (const int *p) : p (p)
{
}
private:
const int *p ;
};
int main (void)
{
const int size = 3 ;
char buffer [size] = {} ;
PtrHolder ph (&size) ;
int **pp = reinterpret_cast <int **> (&ph) ;
std::cout << "*pp: " << *pp << ", pp[0][0]: " << pp[0][0] << "\n" ;
std::cout << "&size: " << &size << ", size: " << size << "\n" ;
pp [0] [0] = 2 ;
std::cout << "*pp: " << *pp << ", pp[0][0]: " << pp[0][0] << "\n" ;
std::cout << "&size: " << &size << ", size: " << size << "\n" ;
return 0 ;
}
Output
*pp: 0041FD28, pp[0][0]: 3
&size: 0041FD28, size: 3
*pp: 0041FD28, pp[0][0]: 2
&size: 0041FD28, size: 3
No comments:
Post a Comment