[gnu.g++.help] Undeliverable Mail

don@zardoz.coral.com (Don Dewar) (01/25/91)

I tried to send this directly to the person, but it failed.  Please
put your address in your mail somewhere.

  ) Return-Path: <help-g++-request@prep.ai.mit.edu>
  ) Date: 21 Jan 91 05:52:08 GMT
  ) From: uunet!csc1.anu.edu.au!csc1!drw900  ("Drew R Whitehouse")
  ) Organization: Australian National University Supercomputer Facility
  ) Subject: help with "new" operator...
  ) Sender: uunet!prep.ai.mit.edu!help-g++-request
  ) To: help-g++@prep.ai.mit.edu
  ) 
  ) 
  ) 	I'm having trouble understanding the functionality of "new" -
  ) why does the following bit of code crash ? Wht isn't the value of p in
  ) the function being returned ? (Yes, I'm new to c++...)
  ) 
  ) 	Any help appreciated.
  ) 							Drew.
  ) 
  ) --------------------------------------------------------------
  ) 
  ) #include <stream.h>
  ) 
  ) class junk 
  ) {
  ) private:
  )     int a;
  ) public:
  )     junk() { a = 42; }
  )     void print (char* mess) { cout << mess << a << "\n"; }
  ) };
  ) 
  ) void allocate(junk*);
  ) 
  ) main()
  ) {
  )     junk* jp;
  )     junk* jj = new junk;
  ) 
  )     allocate(jp);
  ) 
  )     jj->print("allocated in main() - ");
  )     jp->print("allocated by allocate() -");
  ) }
  ) 
  ) void allocate (junk* p)
  ) {
  )     p = new junk;
  
  I believe you problem is here.  It should read :
       p = new junk();
  
  )     p->print("allocated inside allocate() ");
  ) }
  ) 
  ) -------------------------------------------------------------------------
  
  
    +---------+
    | Coral   |
    |@@@@@*@**|
    |@@*@@**@@|     Don Dewar
    |*@@**@@@@|     Coral Network Corporation, Marlborough, MA
    |@***@@@@@|     Internet: don@coral.com
    |@@**@@@@@|     Phone:    (508) 460-6010
    |*********|     Fax:      (508) 481-6258
    |Networks |
    +---------+

End of returned message