Article View: comp.os.os2.programmer
Article #3448Re: malloc() and swapper.dat problems ....
From: David Charlap
Date: Mon, 11 Jan 1993 23:07
Date: Mon, 11 Jan 1993 23:07
45 lines
2218 bytes
2218 bytes
In article <1993Jan11.202609.3711@gandalf.UMCS.Maine.EDU> jurlwin@gandalf.UMCS.Maine.EDU (Jeff Urlwin) writes:
>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);
>}
Actually, although this should work, you should use calloc() to
allocate an array of storage. Instad, use:
p = calloc(10000000, sizeof(int));
if (p == NULL) {
fprintf (stderr, "Malloc failed...\n");
exit(1);
}
It'll have the same effect, but it's closer to proper form. A user
reading this has a clear concept that you're allocating an array of
10000000 ints, and not some generic block of RAM.
By the way, the original program could have been made to work by
changing the original array to one of chars, and that would also work,
since sizeof(char) == 1.
>That should fix it...
>
>By the way, this code *could* fail on any Unix system too...
The original code would fail on any system. One with memory
protection, because only 1/4 of the used memory was allocated. One
without memory protection, because those systems usually don't have
virtual memory, and malloc() probably wouldn't be able to get that
much memory from the system.
Of course, quatas might keep malloc() from working, even if the code
is proper.
--
|) David Charlap | .signature confiscated by FBI due to
/|_ dic5340@hertz.njit.edu | an ongoing investigation into the
((|,) | source of these .signature virusses
~|~
Technical Details
Message-ID:
<1993Jan11.230743.1752@njitgw.njit.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!gatech!rutgers!njitgw.njit.edu!hertz.njit.edu!dic5340References:
<1ipnriINNsm5@im4u.cs.utexas.edu> <1993Jan10.200412.454149@sue.cc.uregina.ca> <1993Jan11.202609.3711@gandalf.UMCS.Maine.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!rutgers!njitgw.njit.edu!hertz.njit.edu!dic5340 From: dic5340@hertz.njit.edu (David Charlap) Newsgroups: comp.os.os2.programmer Subject: Re: malloc() and swapper.dat problems .... Message-ID: <1993Jan11.230743.1752@njitgw.njit.edu> Date: 11 Jan 93 23:07:43 GMT References: <1ipnriINNsm5@im4u.cs.utexas.edu> <1993Jan10.200412.454149@sue.cc.uregina.ca> <1993Jan11.202609.3711@gandalf.UMCS.Maine.EDU> Sender: news@njit.edu Organization: New Jersey Institute of Technology, Newark, N.J. Lines: 45 Nntp-Posting-Host: hertz.njit.edu