Re: Question on the current behaviour of malloc () on Linux

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 21 Dec 2005, Jie Zhang wrote:

> Hi,
>
> I first asked this question on uClinux mailing list. My first question
> is <http://mailman.uclinux.org/pipermail/uclinux-dev/2005-December/036042.html>.
> Although I found this issue on uClinux, it's also can be demostrated
> on Linux. This is a small program:
>

Another FAQ....

> $> cat test2.c
> #include <stdio.h>
> #include <stdlib.h>
>
> int
> main ()
> {
>  char *p;
>  int i, j;
>  for (i = 0;; i++)
>    {
>      p = (char *) malloc (8 * 1024 * 1024);
>      if (p)
>        for (j = 0; j < 8 * 1024 * 1024; j++)
>          p[j] = 0x55;
>      else
>        {
>          printf ("%d fail\n", i);
>          break;
>        }
>    }
>  return 0;
> }
>
> When I run it on my Linux notebook, it will be killed. I expect to see
> it prints out   "fail".

Your expectations are not based upon any logic, only wishes.

>
> Thanks,
> Jie
>


To make your wishes come true execute:
     echo "1" >/proc/sys/vm/overcommit_memory
... as a super-user.

That will make malloc() fail when there isn't any more virtual
memory.

An even shorter program...

main ()
{
     for(;;)
         *(long *)malloc(0x0010000) = 1;
}

This will seg-fault because malloc will eventually return NULL and,
yawn, nothing except your program gets killed.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.55 BogoMips).
Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux