[MPlayer-dev-eng] [RFC] libnut demuxer API
Aurelien Jacobs
aurel at gnuage.org
Fri Sep 9 00:24:37 CEST 2005
On Thu, 8 Sep 2005 11:32:14 +0200
Reimar Döffinger <Reimar.Doeffinger at stud.uni-karlsruhe.de> wrote:
> Hi,
> On Wed, Sep 07, 2005 at 11:44:39PM +0200, Aurelien Jacobs wrote:
> > On Wed, 7 Sep 2005 08:45:59 +0300
> > Oded Shimon <ods15 at ods15.dyndns.org> wrote:
> > > ahem, yes, malloc :)
> > > even if you run out of ram, in linux, malloc STILL doesn't fail :P
> >
> > That's plain wrong !!
> [...]
> > aurel at homer:~/x$ gcc -Wall -o x x.c
> > aurel at homer:~/x$ ./x 100000000
> > 0x2aaaaae02010
> > aurel at homer:~/x$ ./x 1000000000
> > (nil)
>
> You have been testing whether an application can malloc any amout of
> RAM, which of course is not the case.
No ! I was really checking if malloc fails when you have no more
free RAM. If I had more than 1GB free ram, `./x 1000000000` wouldn't
have failed.
But if you want a better example, here it is:
aurel at homer:~/x$ free
total used free shared buffers cached
Mem: 511040 459992 51048 0 19320 196168
-/+ buffers/cache: 244504 266536
Swap: 0 0 0
aurel at homer:~/x$ cat x.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int
main (int argc, char **argv)
{
int s = atoi (argv[1]);
while (1)
{
char *p = malloc (s);
printf ("%p\n", p);
if (!p)
break;
memset (p, 0x66, s);
}
return 0;
}
aurel at homer:~/x$ gcc -Wall -o x x.c
aurel at homer:~/x$ ./x 50000000
0x2aaaaae02010
0x2aaaaddb3010
0x2aaab0d63010
0x2aaab3d13010
0x2aaab6cc3010
(nil)
Here the memset is needed so that the kernel really alloc the memory.
Without it, no memory would be allocated, and so there would always
be enough free ram, malloc wouldn't fail, and you would have an
almost infinite loop.
But still the point is that the kernel will return NULL when he knows
there is not enough memory (and when vm.overcommit_memory=0 or =2,
see Documentation/vm/overcommit-accounting in kernel sources).
So yes, malloc can fail and return NULL.
Aurel
More information about the MPlayer-dev-eng
mailing list