943,693 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1220
  • C++ RSS
Sep 21st, 2006
1

interesting code......Help

Expand Post »
please help me understand the folowing code....
C++ Syntax (Toggle Plain Text)
  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:
Similar Threads
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Sep 21st, 2006
1

Re: interesting code......Help

Click to Expand / Collapse  Quote originally posted by Laiq Ahmed ...
please help me understand the folowing code....
C++ Syntax (Toggle Plain Text)
  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...
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 21st, 2006
0

Re: interesting code......Help

>>cout<< ((A*)3)->get() << endl;

This is using undefined behavor techniques. 3 isprobably not a valid address for an object of type class A.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 21st, 2006
1

Re: interesting code......Help

>>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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 22nd, 2006
1

Re: interesting code......Help

heyz it its undefined what about the following code
C++ Syntax (Toggle Plain Text)
  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 ..........
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Sep 22nd, 2006
0

Re: interesting code......Help

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: adding a '\' to a CString object
Next Thread in C++ Forum Timeline: nested for loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC