Article View: comp.os.msdos.programmer
Article #6631Re: BorlandC++ and dynamic mem allocation
From: Jos Horsmeier
Date: Sun, 22 Nov 1992 14:31
Date: Sun, 22 Nov 1992 14:31
35 lines
1633 bytes
1633 bytes
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
Technical Details
Message-ID:
<3908@dozo.and.nl>Path:
arc1-ca.pugleaf.net!sparky!uunet!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!sun4nl!and!josReferences:
<1992Nov22.014208.2932@samba.oit.unc.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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