home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18980 < prev    next >
Encoding:
Text File  |  1993-01-11  |  1.2 KB  |  43 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!wingnut!pauljo
  3. From: pauljo@microsoft.com (Paul Johns)
  4. Subject: Re: using new
  5. Message-ID: <1993Jan12.002045.23805@microsoft.com>
  6. Date: 12 Jan 93 00:20:45 GMT
  7. Organization: Microsoft Corp., Redmond, Washington, USA
  8. References: <1993Jan8.171950.22884@news.columbia.edu> 
  9. Lines: 32
  10.  
  11. In article <1993Jan8.171950.22884@news.columbia.edu> ae2@cunixa.cc.columbia.edu wrote:
  12. >     I am in the process of porting a C program into C++ and
  13. > learning a lot about C++. I am trying to find an equivalent C++
  14. > construct for the following malloc command:
  15. >         
  16. >     ptr = (TypeCast *) malloc( LENGTH(n));
  17.  
  18. No reason not to continue using malloc here....you can intermix
  19. malloc and new in the same program provided that you free all
  20. blocks allocated with malloc by using free and that you delete
  21. all blocks allocated by using new with delete.
  22.  
  23. > where Length(n) is a macro that will return the n times of the
  24. > desired object size. I have tried the following statement using new
  25. > but for no avail:
  26. >     ptr = (TypeCast *) new char(LENGTH(n));
  27.  
  28. This statement means "allocate ONE character and initialize it
  29. with "LENGTH(n)".
  30.  
  31. You want:
  32.  
  33.     ptr = (TypeCast *) new char [LENGTH(n)];
  34.  
  35. instead....
  36.  
  37. // Paul
  38.  
  39.