home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18456 < prev    next >
Encoding:
Text File  |  1992-12-27  |  2.8 KB  |  66 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!spool.mu.edu!darwin.sura.net!jvnc.net!newsserver.technet.sg!nuscc!papaya!suresh
  3. From: suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar)
  4. Subject: Re: Can't I initialize a dynamically allocated object in its constructor
  5. Message-ID: <1992Dec28.031532.4158@nuscc.nus.sg>
  6. Sender: usenet@nuscc.nus.sg
  7. Reply-To: suresh@iss.nus.sg (Suresh Thennarangam - Research Scholar)
  8. Organization: Institute of Systems Science, NUS, Singapore
  9. References: <1992Dec22.114251.6663@nuscc.nus.sg> <1992Dec27.200209.19143@news2.cis.umn.edu>
  10. Date: Mon, 28 Dec 1992 03:15:32 GMT
  11. Lines: 53
  12.  
  13. In article <1992Dec27.200209.19143@news2.cis.umn.edu> hansen@myria.cs.umn.edu (David Hansen) writes:
  14. >In article <1992Dec22.114251.6663@nuscc.nus.sg>, suresh@papaya.iss.nus.sg (Suresh Thennarangam - Research Scholar) writes:
  15. >[... most of included code deleted ...]
  16. >|> > 
  17. >|> > Vector Vector::operator+ (Vector& a){
  18. >|> >    if (getsize() != a.getsize()){
  19. >|> >       printf("operator '+':Vectors are not of the same size.\n");
  20. >|> >       exit(0);
  21. >|> >    }
  22. >|> >    Vector retVect(getsize()) ;
  23. >|> >    for (int i = 0 ; i < a.getsize() ; i++){
  24. >|> >       retVect.item[i] = item[i] + a.item[i] ;
  25. >|> >    }
  26. >|> >    return retVect ;
  27. >|> > }
  28. >|> > 
  29. >|> My point:
  30. >|> 
  31. >|> retVect used above is a automatic variable that will go out of scope
  32. >|> as soon as the function Vector::operator+() is exited and it's 
  33. >|> destructor will be called which will deallocate the memory
  34. >|> that was allocated by the constructor. Although the returned
  35. >|> Vector object is a copy and will have a valid item pointer, it will 
  36. >|> be pointing to deallocated data.
  37. >
  38. >Ummm, I don't think so.  True, retVect is automatic and will be destroyed when
  39. >it leaves scope.  However, at least on _my_ system (CenterLine ObjectCenter),
  40. >the copy constructor is called _before_ retVect is destroyed.  The copy is
  41. >valid.  I would hope this is the way it is supposed to work...
  42. >
  43. >What you describe is the problem that existed _before_ the copy constructor
  44. >was added.  Since none was defined, the compiler generated one which simply
  45. >performed a memberwise initialization of the copy.  Since one of the members
  46. >is a pointer to some memory which is then deallocated before the copy is used,
  47. >we have the classic "dangling pointer."  The copy constructor allocates its
  48. >own memory for the pointer, which is then freed when the temporary is destroyed.
  49. >
  50. >Or is there something else I'm missing?
  51.  
  52. I think you are right. I tried it out with my cfront2.1  on SUN4 and it does 
  53. work. I omitted to realize that the copy constructor would be called before
  54. the automatic variable retVect went out of scope and that is really the way
  55. it should work. 
  56.  
  57. Regards,
  58.  
  59.  
  60.       __                  
  61.      (_   / /  o_   o  o |_
  62.      __)/(_( __) (_(_ /_)| )_
  63.  
  64.  
  65.  
  66.