| | |
interesting code......Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
please help me understand the folowing code....
this code runs! and prints the correct output if i return zero in get() function,, but when i return a it throws an exception ( i think becuase a is not initialized, infact not constructed ), but this code runs ......no idea:eek:
C++ Syntax (Toggle Plain Text)
class A { int a; public: A() : a ( 0 ) {} int get() { cout << "Hello WOrld " << endl; return 0; } }; int main( int argc, char** argv ) { cout<< ((A*)3)->get() << endl; return 0; }
•
•
•
•
please help me understand the folowing code....
C++ Syntax (Toggle Plain Text)
class A { int a; public: A() : a ( 0 ) {} int get() { cout << "Hello WOrld " << endl; return 0; } }; int main( int argc, char** argv ) { cout<< ((A*)3)->get() << endl; return 0; }
So as a pointer to object is created, you dont specifically allocate any memory, the class variable doesnt exist. So as soon as the function si encountered, the "cout" stmt is executed and prints out the output but when encounters the reutrn a stmt, there is as such no "a" to which the object points to since no memory has been allocated.
In the end, yes since the memory is not allocated for the pointer and neither does it point to any object hence an execption is generated. Returning something which doesnt exist is not a very good thing

And btw your code doesnt technically run as an run time exception is generated, there is a difference you know

Hope it helped, bye.
Last edited by ~s.o.s~; Sep 21st, 2006 at 1:21 pm. Reason: Some additions...
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
>>cout<< ((A*)3)->get() << endl;
This is using undefined behavor techniques. 3 isprobably not a valid address for an object of type class A.
Another way to do it is to make the pointer point to an already existing object of the class
This is using undefined behavor techniques. 3 isprobably not a valid address for an object of type class A.
C++ Syntax (Toggle Plain Text)
A* pA = new A; // allocate an instance of the class pA->get(); // // and don't forget to deallocate it delete pA;
Another way to do it is to make the pointer point to an already existing object of the class
C++ Syntax (Toggle Plain Text)
A a; // an instance of class A A* pA = &a; // pointer to the object // // // you do NOT delete the pointer
Last edited by Ancient Dragon; Sep 21st, 2006 at 1:26 pm.
•
•
•
•
>>cout<< ((A*)3)->get() << endl;
This is using undefined behavor techniques. 3 isprobably not a valid address for an object of type class A.
Rule:
1. Before using any pointer dont forget to allocate memory to it or to point it to an existing object. Hence when using a pointer better ground it (make it NULL) so that you dont end up using it by mistake.
2. Delete only those pointers for those you have explicitly allocated memory using the "new" stmt. Forgettting to free the memory results in what is known as a memory leak.
Hope it helped, bye.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
heyz it its undefined what about the following code
it works ..........
C++ Syntax (Toggle Plain Text)
class A { int a; public: A() : a ( 0 ) {} int get() const { cout <<" in A()" << endl; return a; } }; int main() { *((int*)0x00100ff)=14; cout<< reinterpret_cast<A*>(0x00100ff)->get() << endl; return 0; }
it works ..........
![]() |
Similar Threads
- plotting cpu and mem consumption (C++)
- STL <map> question? (C++)
- Sprintf and buffer overflow (C)
- Playing cards (C++)
- str error (C++)
Other Threads in the C++ Forum
- Previous Thread: adding a '\' to a CString object
- Next Thread: nested for loops
Views: 1062 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






