Advertisement
Archon

Member function of a nonexistent object

Feb 13th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. class C{
  4. public:
  5.     int x;
  6.     void f(){
  7.         printf("\nHello world!\n");
  8.         printf("this == %p\n", this);
  9.         printf("x == %d\n", x);
  10.     }
  11. };
  12.  
  13. int main(){
  14.     C c;
  15.     c.f();
  16.  
  17.     C *p = reinterpret_cast<C*>(0xfe58648); //nonsense address
  18.     p->f();
  19. }
  20.  
  21. //Output:
  22.  
  23. Hello world!
  24. this == 0x7fff656e3a40
  25. x == 0
  26.  
  27. Hello world!
  28. this == 0xfe58648
  29. Segmentation fault: 11
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement