moll@umn-cs.CS.UMN.EDU (Rick Moll) (11/14/88)
/*
I have a problem with malloc in Borland's Turbo C v1.5. The program
below works fine using the Small data model, but hangs my system (I
have to reboot) when compiled under the Compact model.
I don't do anything different except change the memory model (on the
menu) to "Compact" and force a re-compile; the manual says that the
linker will automatically pull in the right libraries.
Can anyone tell me what I'm doing wrong? Is this program legitimate C
code? Can someone with v1.5 try this and see if it does the same
thing on your system? How about v2.0? Thanks in advance.
*/
void main()
{
void *inbuf;
inbuf=(void*) (malloc(100));
strcpy(inbuf, "This text will overwrite something important, no doubt!");
exit(0);
}
--
Richard J. Moll ...rutgers!umn-cs!moll
moll@umn-cs.cs.umn.edu
nelson@sun.soe.clarkson.edu (Russ Nelson) (11/14/88)
In article <10040@umn-cs.CS.UMN.EDU> moll@umn-cs.CS.UMN.EDU (Rick Moll) writes:
inbuf=(void*) (malloc(100));
Functions default to int. You are casting a 16 bit int into a 32 bit pointer.
Never assume that sizeof(int) == sizeof(void *).
--
--russ (nelson@clutx [.bitnet | .clarkson.edu])
To surrender is to remain in the hands of barbarians for the rest of my life.
To fight is to leave my bones exposed in the desert waste.
williamo@hpcupt1.HP.COM (William O'Shaughnessy) (11/15/88)
I'm would guess that your (void *) is meerly type coecing an integer(2 bytes) into a 4 byte pointer. This does not work in the compact model where pointers are 4 bytes. Try adding the declaration extern void *malloc(); to your program. It is good to remember the rule in C that things always default to type int if not specified. Good Luck, and as always no guarantees are given or implied for any of the above. The opinions given above are my personal opinions and not those of my employer. Bill O'Shaughnessy