home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / os / msdos / programm / 10846 < prev    next >
Encoding:
Internet Message Format  |  1992-11-22  |  1.6 KB

  1. Path: sparky!uunet!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: BorlandC++ and dynamic mem allocation
  5. Message-ID: <3908@dozo.and.nl>
  6. Date: 22 Nov 92 14:31:52 GMT
  7. References: <1992Nov22.014208.2932@samba.oit.unc.edu>
  8. Organization: AND Software BV Rotterdam
  9. Lines: 35
  10.  
  11. In article <1992Nov22.014208.2932@samba.oit.unc.edu> jdmce@med.unc.edu (Duncan McEwen) writes:
  12.  
  13. [ ... ]
  14.  
  15. |So, I switched to the the "compact" compiler option.  Again the
  16. |program worked ok, but if I increased numInRange (see Malloc)
  17. |to around 10000, the program misbehaved and seemed to wipe out
  18. |part of resident command.com.  That is, the program ran ok but
  19. |dos was then not able to load command.com
  20. |
  21. |Does anyone have an hints as to something obvious I may be missing
  22. |that could acount for dynamically allocated memory being so naughty?
  23.  
  24. [ Deleted all but the relevant parts ... ]
  25.  
  26. |      /*   initialize some variables  */
  27. |    if ((primArray = (long *) malloc(numInRange)) == NULL)
  28.  
  29.            /* ... */
  30.  
  31. |    for (i = 0; i < numInRange; ++i)
  32. |           *(primArray+i) = firstInRange++;
  33.  
  34. You're allocating numInRange bytes, but you need numInRange*sizeof(long)
  35. bytes. A typical sizeof(long) is four bytes, so you only allocated a
  36. quarter of what you really needed, thereby destroying memory that
  37. doesn't belong to your process. Try this:
  38.  
  39.     if ((primArray = (long *) malloc(numInRange*sizeof(long))) == NULL)
  40.  
  41. BTW, *(primArray+i) is just an ugly way (IMHO) of expressing primArray[i].
  42.  
  43. kind regards,
  44.  
  45. Jos aka jos@and.nl
  46.