home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / msdos / programm / 9316 < prev    next >
Encoding:
Internet Message Format  |  1992-09-12  |  2.3 KB

  1. Path: sparky!uunet!spool.mu.edu!sdd.hp.com!cs.utexas.edu!ut-emx!jamshid
  2. From: jamshid@ut-emx.uucp (Jamshid Afshar)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: BC++ Far new()
  5. Summary: if you need to cast, it's probably wrong; "new far char[100]"
  6. Message-ID: <79483@ut-emx.uucp>
  7. Date: 10 Sep 92 19:44:06 GMT
  8. References: <18j0e6INN8ju@usenet.INS.CWRU.Edu> <gin503.715957220@cdc835> <18j1t9INNbs3@usenet.INS.CWRU.Edu>
  9. Reply-To: jamshid@emx.utexas.edu
  10. Organization: The University of Texas at Austin; Austin, Texas
  11. Lines: 39
  12.  
  13. In article <18j1t9INNbs3@usenet.INS.CWRU.Edu> bu254@cleveland.Freenet.Edu (Stephen Groundwater) writes:
  14. >>Question : how can I call the far version of 'new()' from a small memory
  15. >>           program ?
  16. >
  17. >char far* MyBuf;
  18. >MyBuf=(char far *)new[100L];
  19. >Voila, 100 bytes of far memory, the important bit is the L.
  20.  
  21. When I first saw this I thought it contained a syntax error because
  22. you didn't specify the type (eg, "new int[100L]").  I'm still not sure
  23. whether its legal but BC++ 3.1 does accept it.  The code is still
  24. wrong, though, because BC++ assumes you're allocating 100 'int's.  The
  25. fact that you had to cast should have tipped you off that something is
  26. wrong with the code.  Unlike malloc(), 'new' always returns a pointer
  27. of the correct type.
  28.     char far* MyBuf = new char[100L]
  29.     // calls 'void far* operator new(unsigned long)' in all memory models
  30.  
  31. I would recommend using a different, more explicit syntax, though:
  32.     char far* MyBuf = new far char[100];
  33.     // calls 'void far* operator new(unsigned long)' in small&medium,
  34.     // calls 'void* operator new(size_t/*unsigned int*/)' in large&huge
  35.     //   (pointers/heap are 'far' in compact, large and huge models) */
  36.  
  37. You can also use this syntax to allocate 'huge' objects (objects over
  38. 64K requiring 'huge' pointers):
  39.     char huge* MyBuf = new huge char[80000];
  40.     // always calls 'void far* operator new(unsigned long)'
  41.  
  42. Remember, though, that anytime you use the 'far' or 'huge' keywords,
  43. you're using a non-portable (even to other MS-DOS compilers) syntax,
  44. and that it isn't thoroughly documented (actually, I don't think this
  45. 'new' stuff is documented at all).  Compile with and heed all
  46. warnings, and make sure you know what you're doing if you put in a
  47. cast.  Also, BC++ 3.1 users, make sure you have the 'operator new()'
  48. patches in BC31P1.ZIP from Simtel's BORLANDC directory.
  49.  
  50. Jamshid Afshar
  51. jamshid@emx.utexas.edu
  52.