home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12647 < prev    next >
Encoding:
Internet Message Format  |  1992-08-21  |  1.5 KB

  1. Path: sparky!uunet!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Large memory problem
  5. Message-ID: <3274@dozo.and.nl>
  6. Date: 22 Aug 92 10:32:02 GMT
  7. References: <92234.114605U37956@uicvm.uic.edu>
  8. Organization: AND Software BV Rotterdam
  9. Lines: 39
  10.  
  11. In article <92234.114605U37956@uicvm.uic.edu> U37956@uicvm.uic.edu writes:
  12. |I am using Ms C 6.0 on DOS5.0 . I have 4MB extented memory.
  13. |The following C code is causing me trouble:
  14. |
  15. |#include <stdio.h>
  16. |main()
  17. |$ int i;
  18. |  unsigned char *buffer;
  19. |
  20. |  buffer=(unsigned char*)malloc(480000);
  21. |  for(i=0; i<480000; i++)
  22. |   $
  23. |       buffer[i]= i%256;
  24. |       printf("%u\n", buffer[i]);
  25. |
  26. | It is compiled in large(or huge) memory model:
  27. |
  28. |     cl /AL myprog.c
  29. |
  30. |When I run the program, it prints out the numbers, but in the end, it
  31. |gives message" memory allocation error, Can't load COMMAND, the system
  32. |halted.", and then the system dies.
  33. |
  34. |What's wrong ?
  35.  
  36. Intel and Microsoft are wrong ... ;-) No seriously, the type of
  37. the parameter for malloc is size_t. A size_t type is defined as
  38. an unsigned integral. Integers are 16 bits wide in Microsofts'
  39. C implementation, so the largest block of memory you can allocate
  40. is just 64K bytes. You're trying to allocate much more than that.
  41.  
  42. Didn't the compiler warn you for it? I assume that malloc allocates
  43. something like 480000 modulo 64K bytes or something. Therefore you
  44. ruin your heap and everything when you're trying to assign values to
  45. elements in that block. 
  46.  
  47. kind regards,
  48.  
  49. Jos aka jos@and.nl
  50.