home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / os / os2 / programm / 7500 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.7 KB  |  96 lines

  1. Newsgroups: comp.os.os2.programmer
  2. Path: sparky!uunet!noc.near.net!saturn.caps.maine.edu!gandalf!jurlwin
  3. From: jurlwin@gandalf.UMCS.Maine.EDU (Jeff Urlwin)
  4. Subject: Re: malloc() and swapper.dat problems ....
  5. Message-ID: <1993Jan11.202609.3711@gandalf.UMCS.Maine.EDU>
  6. Organization: University of Maine, Department of Computer Science
  7. References: <1ipnriINNsm5@im4u.cs.utexas.edu> <1993Jan10.200412.454149@sue.cc.uregina.ca>
  8. Date: Mon, 11 Jan 1993 20:26:09 GMT
  9. Lines: 85
  10.  
  11. In article <1993Jan10.200412.454149@sue.cc.uregina.ca> skagos@mercury.cs.uregina.ca (Takis Skagos) writes:
  12. >Hi,
  13. >
  14. >  I was curious to see how OS/2 handled malloc()ing big chunks of
  15. >memory, so I wrote a quick and really-dirty program last night to
  16. >test out some stuff.  Anyway, the program dumped core before
  17. >completing.  Here is a copy of the code and following that is a
  18. >copy of what was displayed when the program dumped.  By the way,
  19. >I used "icc -c-" and "icc -c- -gd+" to compile the program, but the
  20. >same thing happened either way.
  21.  
  22. Yes, it will.  Basically, you are not "malloc"ing enough space to skip
  23. through 10 million integers.  You should really try:
  24.  
  25. p = malloc(10000000 * sizeof(int));
  26. if (p == NULL)
  27. {
  28.   fprintf(stderr, "Malloc failed to allocate space...\n");
  29.   exit(1);
  30. }
  31.  
  32. (which will, using CSet/2, allocate 40 Million bytes...)  The sizeof(int)
  33. allows the compiler to tell you how much it needs for each int size 
  34. variable.  This will allow you to move the code to any platform, assuming,
  35. of course, that you can allocate 10 million of these items... The if statement
  36. should help in that situation...
  37.  
  38. OS/2 helps protect you from going past your own executable's bounds in
  39. memory (like any GOOD protected OS...).  Essentially, when your program
  40. tries to assign p[k] = 1 when k >= 3,000,000, you are out of bounds of
  41. what you allocated.  The system could "dump core" anywhere after you are
  42. out of bounds, but MAY not (if you don't go out of your executable's 
  43. address space).  
  44.  
  45. A nice, safe, way to handle this could be:
  46.  
  47. #define MYMAX 10000000
  48. ...
  49. p = malloc(MYMAX * sizeof(int)); 
  50. if (p == NULL)
  51. {
  52.   fprintf(stderr, "Malloc failed to allocate space for %d items\n", MYMAX);
  53.   exit(1);
  54. }
  55. ...
  56. while(k < MYMAX)
  57. ....
  58. >
  59. >---source code---
  60. >
  61. >#include <stdio.h>
  62. >
  63. >main()
  64. >{
  65. >  int k = 0;
  66. >  int *p;
  67. >  setbuf(stdout,NULL); setbuf(stdin,NULL);
  68. >  p = (int *) malloc(12000000);  /* bytes */
  69. >  printf("\n malloced 12000000 bytes.");
  70. >  while(k <           10000000 )
  71. >  {
  72. >    p[k] = 1;
  73. >    k++;
  74. >    if(k % 10000 == 0)
  75. >      putchar('.');
  76. >  }
  77. >  free(p);
  78. >  printf("\n freed 12000000 bytes.\n");
  79. >}
  80. >
  81. >---output---
  82. >
  83.  
  84. That should fix it...
  85.  
  86. By the way, this code *could* fail on any Unix system too...
  87.  
  88. Jeff
  89.  
  90.  
  91.  
  92. -- 
  93. --------------------------------------
  94. jurlwin@gandalf.umcs.maine.edu
  95.  
  96.