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

  1. Nntp-Posting-Host: solva.ifi.uio.no
  2. Newsgroups: comp.lang.c++
  3. Path: sparky!uunet!psinntp!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
  4. From: jar@solva.ifi.uio.no (Jo Are Rosland)
  5. Subject: Re: using new
  6. In-Reply-To: ae2@cunixa.cc.columbia.edu's message of Fri, 8 Jan 1993 17:19:50 GMT
  7. Message-ID: <JAR.93Jan10164850@solva.ifi.uio.no>
  8. X-Md4-Signature: f92980b69468f75c25c2139d72153b3e
  9. Sender: jar@ifi.uio.no (Jo Are Rosland)
  10. Organization: Dept. of Informatics, University of Oslo, Norway
  11. References: <1993Jan8.171950.22884@news.columbia.edu>
  12. Date: Sun, 10 Jan 1993 15:48:50 GMT
  13. Lines: 31
  14. Originator: jar@solva.ifi.uio.no
  15.  
  16. In article <1993Jan8.171950.22884@news.columbia.edu> ae2@cunixa.cc.columbia.edu (Amiran Eliashvili) writes:
  17.        I am in the process of porting a C program into C++ and
  18.    learning a lot about C++. I am trying to find an equivalent C++
  19.    construct for the following malloc command:
  20.  
  21.        ptr = (TypeCast *) malloc( LENGTH(n));
  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.  
  27.        ptr = (TypeCast *) new char(LENGTH(n));
  28.  
  29. This is really a question of the kind you'd find answered in any C++
  30. tutorial book.  Your C++ should probably look like
  31.  
  32.     ptr = new TypeCast[n];
  33.  
  34. which means "allocate enough space for an array of n elements of type
  35. TypeCast, then return a pointer to it of type TypeCast*".
  36.  
  37. The corresponding free() statement should look like
  38.  
  39.     delete [] ptr;
  40.  
  41. where the [] are necessary since you've allocated an array in the
  42. first place.
  43.  
  44. --
  45. Jo Are Rosland
  46. jar@ifi.uio.no
  47.