interesting code......Help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

interesting code......Help

 
1
  #1
Sep 21st, 2006
please help me understand the folowing code....
  1. class A {
  2. int a;
  3. public:
  4. A() : a ( 0 ) {}
  5. int get() {
  6. cout << "Hello WOrld " << endl;
  7. return 0;
  8. }
  9. };
  10.  
  11. int main( int argc, char** argv )
  12. {
  13. cout<< ((A*)3)->get() << endl;
  14. return 0;
  15. }
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:
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: interesting code......Help

 
1
  #2
Sep 21st, 2006
Originally Posted by Laiq Ahmed View Post
please help me understand the folowing code....
  1. class A {
  2. int a;
  3. public:
  4. A() : a ( 0 ) {}
  5. int get() {
  6. cout << "Hello WOrld " << endl;
  7. return 0;
  8. }
  9. };
  10.  
  11. int main( int argc, char** argv )
  12. {
  13. cout<< ((A*)3)->get() << endl;
  14. return 0;
  15. }
The thing is that when an object is created, its constructor is called but not when the object pointer is created which is the thing in your case. (please get this verified, havent done C++ in quite a while)

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,652
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1499
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: interesting code......Help

 
0
  #3
Sep 21st, 2006
>>cout<< ((A*)3)->get() << endl;

This is using undefined behavor techniques. 3 isprobably not a valid address for an object of type class A.
  1. A* pA = new A; // allocate an instance of the class
  2. pA->get();
  3. //
  4. // and don't forget to deallocate it
  5. delete pA;

Another way to do it is to make the pointer point to an already existing object of the class
  1. A a; // an instance of class A
  2. A* pA = &a; // pointer to the object
  3. //
  4. //
  5. // you do NOT delete the pointer
Last edited by Ancient Dragon; Sep 21st, 2006 at 1:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: interesting code......Help

 
1
  #4
Sep 21st, 2006
Originally Posted by Ancient Dragon View Post
>>cout<< ((A*)3)->get() << endl;

This is using undefined behavor techniques. 3 isprobably not a valid address for an object of type class A.
Oh yes and i overlooked this particular thing. Thanks for pointing it out Mr. Dragon. Maybe this is one of those quizzies questions where you are asked to predict the output of the code snippet.

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: interesting code......Help

 
1
  #5
Sep 22nd, 2006
heyz it its undefined what about the following code
  1.  
  2. class A {
  3. int a;
  4. public:
  5. A() : a ( 0 ) {}
  6.  
  7. int get() const {
  8. cout <<" in A()" << endl;
  9. return a;
  10. }
  11. };
  12.  
  13. int main()
  14. {
  15. *((int*)0x00100ff)=14;
  16. cout<< reinterpret_cast<A*>(0x00100ff)->get() << endl;
  17. return 0;
  18. }

it works ..........
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,652
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1499
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: interesting code......Help

 
0
  #6
Sep 22nd, 2006
yes its using undefined behavior too. You can't just hardcode an address into the program like that and expect it to work because only the computer knows where an object's address, unless you are using some obscure operating system such as some embedded systems.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1062 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC