home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: Large memory problem
- Message-ID: <3274@dozo.and.nl>
- Date: 22 Aug 92 10:32:02 GMT
- References: <92234.114605U37956@uicvm.uic.edu>
- Organization: AND Software BV Rotterdam
- Lines: 39
-
- In article <92234.114605U37956@uicvm.uic.edu> U37956@uicvm.uic.edu writes:
- |I am using Ms C 6.0 on DOS5.0 . I have 4MB extented memory.
- |The following C code is causing me trouble:
- |
- |#include <stdio.h>
- |main()
- |$ int i;
- | unsigned char *buffer;
- |
- | buffer=(unsigned char*)malloc(480000);
- | for(i=0; i<480000; i++)
- | $
- | buffer[i]= i%256;
- | printf("%u\n", buffer[i]);
- |
- | It is compiled in large(or huge) memory model:
- |
- | cl /AL myprog.c
- |
- |When I run the program, it prints out the numbers, but in the end, it
- |gives message" memory allocation error, Can't load COMMAND, the system
- |halted.", and then the system dies.
- |
- |What's wrong ?
-
- Intel and Microsoft are wrong ... ;-) No seriously, the type of
- the parameter for malloc is size_t. A size_t type is defined as
- an unsigned integral. Integers are 16 bits wide in Microsofts'
- C implementation, so the largest block of memory you can allocate
- is just 64K bytes. You're trying to allocate much more than that.
-
- Didn't the compiler warn you for it? I assume that malloc allocates
- something like 480000 modulo 64K bytes or something. Therefore you
- ruin your heap and everything when you're trying to assign values to
- elements in that block.
-
- kind regards,
-
- Jos aka jos@and.nl
-