home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.os.msdos.programmer
- Subject: Re: BorlandC++ and dynamic mem allocation
- Message-ID: <3908@dozo.and.nl>
- Date: 22 Nov 92 14:31:52 GMT
- References: <1992Nov22.014208.2932@samba.oit.unc.edu>
- Organization: AND Software BV Rotterdam
- Lines: 35
-
- In article <1992Nov22.014208.2932@samba.oit.unc.edu> jdmce@med.unc.edu (Duncan McEwen) writes:
-
- [ ... ]
-
- |So, I switched to the the "compact" compiler option. Again the
- |program worked ok, but if I increased numInRange (see Malloc)
- |to around 10000, the program misbehaved and seemed to wipe out
- |part of resident command.com. That is, the program ran ok but
- |dos was then not able to load command.com
- |
- |Does anyone have an hints as to something obvious I may be missing
- |that could acount for dynamically allocated memory being so naughty?
-
- [ Deleted all but the relevant parts ... ]
-
- | /* initialize some variables */
- | if ((primArray = (long *) malloc(numInRange)) == NULL)
-
- /* ... */
-
- | for (i = 0; i < numInRange; ++i)
- | *(primArray+i) = firstInRange++;
-
- You're allocating numInRange bytes, but you need numInRange*sizeof(long)
- bytes. A typical sizeof(long) is four bytes, so you only allocated a
- quarter of what you really needed, thereby destroying memory that
- doesn't belong to your process. Try this:
-
- if ((primArray = (long *) malloc(numInRange*sizeof(long))) == NULL)
-
- BTW, *(primArray+i) is just an ugly way (IMHO) of expressing primArray[i].
-
- kind regards,
-
- Jos aka jos@and.nl
-