🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.os2.programmer
Article #3440

Re: malloc() and swapper.dat problems ....

#3440
From: Jeff Urlwin
Date: Mon, 11 Jan 1993 20:26
84 lines
2872 bytes
In article <1993Jan10.200412.454149@sue.cc.uregina.ca> skagos@mercury.cs.uregina.ca (Takis Skagos) writes:
>Hi,
>
>  I was curious to see how OS/2 handled malloc()ing big chunks of
>memory, so I wrote a quick and really-dirty program last night to
>test out some stuff.  Anyway, the program dumped core before
>completing.  Here is a copy of the code and following that is a
>copy of what was displayed when the program dumped.  By the way,
>I used "icc -c-" and "icc -c- -gd+" to compile the program, but the
>same thing happened either way.

Yes, it will.  Basically, you are not "malloc"ing enough space to skip
through 10 million integers.  You should really try:

p = malloc(10000000 * sizeof(int));
if (p == NULL)
{
  fprintf(stderr, "Malloc failed to allocate space...\n");
  exit(1);
}

(which will, using CSet/2, allocate 40 Million bytes...)  The sizeof(int)
allows the compiler to tell you how much it needs for each int size 
variable.  This will allow you to move the code to any platform, assuming,
of course, that you can allocate 10 million of these items... The if statement
should help in that situation...

OS/2 helps protect you from going past your own executable's bounds in
memory (like any GOOD protected OS...).  Essentially, when your program
tries to assign p[k] = 1 when k >= 3,000,000, you are out of bounds of
what you allocated.  The system could "dump core" anywhere after you are
out of bounds, but MAY not (if you don't go out of your executable's 
address space).  

A nice, safe, way to handle this could be:

#define MYMAX 10000000
...
p = malloc(MYMAX * sizeof(int)); 
if (p == NULL)
{
  fprintf(stderr, "Malloc failed to allocate space for %d items\n", MYMAX);
  exit(1);
}
...
while(k < MYMAX)
....
>
>---source code---
>
>#include <stdio.h>
>
>main()
>{
>  int k = 0;
>  int *p;
>  setbuf(stdout,NULL); setbuf(stdin,NULL);
>  p = (int *) malloc(12000000);  /* bytes */
>  printf("\n malloced 12000000 bytes.");
>  while(k <           10000000 )
>  {
>    p[k] = 1;
>    k++;
>    if(k % 10000 == 0)
>      putchar('.');
>  }
>  free(p);
>  printf("\n freed 12000000 bytes.\n");
>}
>
>---output---
>

That should fix it...

By the way, this code *could* fail on any Unix system too...

Jeff



-- 
--------------------------------------
jurlwin@gandalf.umcs.maine.edu
Technical Details
Message-ID: <1993Jan11.202609.3711@gandalf.UMCS.Maine.EDU>
Path: arc1-ca.pugleaf.net!sparky!uunet!noc.near.net!saturn.caps.maine.edu!gandalf!jurlwin
References: <1ipnriINNsm5@im4u.cs.utexas.edu> <1993Jan10.200412.454149@sue.cc.uregina.ca>

Show Full Headers
Complete Article Headers
Newsgroups: comp.os.os2.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!noc.near.net!saturn.caps.maine.edu!gandalf!jurlwin
From: jurlwin@gandalf.UMCS.Maine.EDU (Jeff Urlwin)
Subject: Re: malloc() and swapper.dat problems ....
Message-ID: <1993Jan11.202609.3711@gandalf.UMCS.Maine.EDU>
Organization: University of Maine, Department of Computer Science
References: <1ipnriINNsm5@im4u.cs.utexas.edu> <1993Jan10.200412.454149@sue.cc.uregina.ca>
Date: Mon, 11 Jan 1993 20:26:09 GMT
Lines: 85