even this simple code?? c'mon c++

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

Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

even this simple code?? c'mon c++

 
0
  #1
May 10th, 2009
I have this code:
in my judgement it should print 1 and 2, instead it prints 0 and 1
some advice pls... Im using dev c++




  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6. A()
  7. {
  8. arr = new int[2];
  9. }
  10. ~A(){ delete [] arr; }
  11.  
  12. int * arr ;
  13. };
  14.  
  15.  
  16. int main()
  17. {
  18. int * n ;
  19.  
  20. {//induced code block
  21. A aObj;
  22. aObj.arr[0] = 1;
  23. aObj.arr[1] = 2;
  24. n = aObj.arr;
  25.  
  26. /*
  27.  BTW I assume n now points to the same add of aObj.arr ... am I wrong??
  28. */
  29. }
  30.  
  31. std::cout << "\n" << (*n) << " " << (*n+1);
  32. system("pause");
  33. }
Last edited by Ancient Dragon; May 11th, 2009 at 12:20 am. Reason: add line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
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: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: even this simple code?? c'mon c++

 
0
  #2
May 11th, 2009
The problem is scoping -- object A goes out of scope before the cout statement on line 31. Delete the { and } on lines 20 and 29 and your program will work as you expect it to.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: even this simple code?? c'mon c++

 
0
  #3
May 11th, 2009
good, thanks I should've seen that
the morale is dont let garbage printed on the screen fool you even if it looks extremly similar too what you expect
Last edited by namehere05; May 11th, 2009 at 12:28 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,820
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: even this simple code?? c'mon c++

 
0
  #4
May 11th, 2009
Keep in mind that this line (see red part):

std::cout << "\n" << (*n) << " " << (*n+1);

displays aObj.arr[0] + 1 , not aObj.arr[1] . In your case, they are the same, but if you change line 23 to equal something other than 2, you'll still get 1 2 as output.
Last edited by VernonDozier; May 11th, 2009 at 12:33 am.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC