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

  1. Newsgroups: comp.os.os2.programmer
  2. Path: sparky!uunet!wupost!csus.edu!netcom.com!stewarta
  3. From: stewarta@netcom.com (Alex Stewart)
  4. Subject: Re: malloc() and swapper.dat problems ....
  5. Message-ID: <1993Jan11.090948.16559@netcom.com>
  6. Organization: Netcom - Online Communication Services  (408 241-9760 guest) 
  7. X-Newsreader: Tin 1.1 PL5
  8. References: <1993Jan10.200412.454149@sue.cc.uregina.ca>
  9. Date: Mon, 11 Jan 1993 09:09:48 GMT
  10. Lines: 39
  11.  
  12. Takis Skagos (skagos@mercury.cs.uregina.ca) wrote:
  13. : ---source code---
  14. : #include <stdio.h>
  15. : main()
  16. : {
  17. :   int k = 0;
  18. :   int *p;
  19.     ^^^ <-- (depends on the compiler, but at _least_ two bytes (I suspect 4 in
  20.              this case))
  21.  
  22. :   setbuf(stdout,NULL); setbuf(stdin,NULL);
  23. :   p = (int *) malloc(12000000);  /* bytes */
  24. :   printf("\n malloced 12000000 bytes.");
  25. :   while(k <           10000000 )
  26.                         ^^^^^^^^ <-- (10000000*4 bytes = 40000000 > 12000000)
  27.  
  28. :   {
  29. :     p[k] = 1;
  30. :     k++;
  31. :     if(k % 10000 == 0)
  32. :       putchar('.');
  33. :   }
  34. :   free(p);
  35. :   printf("\n freed 12000000 bytes.\n");
  36. : }
  37.  
  38. Counting your dots, it looks like it got to about k=3000000.  Multiply that by
  39. 4 (the size of an int), and you get 12000000, or exactly the size of your 
  40. malloc.  Try making p into a char*.
  41.  
  42. -alex
  43. -- 
  44. -----------------------------------------------------------------------------
  45. Alex Stewart:         Sysop of YBBS (510) 689-8952 |   This space for rent.
  46. stewarta@netcom.com   .----------------------------'-------------------------
  47. stewarta@carleton.edu | Somebody, somewhere, is offended by what I am saying.
  48. -----------------------------------------------------------------------------
  49.