home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / os / os2 / programm / 7509 < prev    next >
Encoding:
Internet Message Format  |  1993-01-11  |  2.1 KB

  1. Path: sparky!uunet!gatech!rutgers!njitgw.njit.edu!hertz.njit.edu!dic5340
  2. From: dic5340@hertz.njit.edu (David Charlap)
  3. Newsgroups: comp.os.os2.programmer
  4. Subject: Re: malloc() and swapper.dat problems ....
  5. Message-ID: <1993Jan11.230743.1752@njitgw.njit.edu>
  6. Date: 11 Jan 93 23:07:43 GMT
  7. References: <1ipnriINNsm5@im4u.cs.utexas.edu> <1993Jan10.200412.454149@sue.cc.uregina.ca> <1993Jan11.202609.3711@gandalf.UMCS.Maine.EDU>
  8. Sender: news@njit.edu
  9. Organization: New Jersey Institute of Technology, Newark, N.J.
  10. Lines: 45
  11. Nntp-Posting-Host: hertz.njit.edu
  12.  
  13. In article <1993Jan11.202609.3711@gandalf.UMCS.Maine.EDU> jurlwin@gandalf.UMCS.Maine.EDU (Jeff Urlwin) writes:
  14. >Yes, it will.  Basically, you are not "malloc"ing enough space to skip
  15. >through 10 million integers.  You should really try:
  16. >
  17. >p = malloc(10000000 * sizeof(int));
  18. >if (p == NULL)
  19. >{
  20. >  fprintf(stderr, "Malloc failed to allocate space...\n");
  21. >  exit(1);
  22. >}
  23.  
  24. Actually, although this should work, you should use calloc() to
  25. allocate an array of storage.  Instad, use:
  26.  
  27. p = calloc(10000000, sizeof(int));
  28. if (p == NULL) {
  29.   fprintf (stderr, "Malloc failed...\n");
  30.   exit(1);
  31. }
  32.  
  33. It'll have the same effect, but it's closer to proper form.  A user
  34. reading this has a clear concept that you're allocating an array of
  35. 10000000 ints, and not some generic block of RAM.
  36.  
  37. By the way, the original program could have been made to work by
  38. changing the original array to one of chars, and that would also work,
  39. since sizeof(char) == 1.
  40.  
  41. >That should fix it...
  42. >
  43. >By the way, this code *could* fail on any Unix system too...
  44.  
  45. The original code would fail on any system.  One with memory
  46. protection, because only 1/4 of the used memory was allocated.  One
  47. without memory protection, because those systems usually don't have
  48. virtual memory, and malloc() probably wouldn't be able to get that
  49. much memory from the system.
  50.  
  51. Of course, quatas might keep malloc() from working, even if the code
  52. is proper.
  53. -- 
  54.   |)  David Charlap          | .signature confiscated by FBI due to
  55.  /|_  dic5340@hertz.njit.edu | an ongoing investigation into the
  56. ((|,)                        | source of these .signature virusses
  57.  ~|~
  58.