require this C malloc program analysis

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kernel.lover

    require this C malloc program analysis

    hello,
    I download this code from worx site with some of my
    modification appear below. i want to ask why this program fails to run
    after megs_obtained with 660 value?
    Also as it concern with avail memory allocation, how more that
    256MB memory be given to this program?
    I am using Fedora Core 1 with 256 physical RAM and 80GB HDD.
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>

    #define ONE_K (1024)

    int main() {
    char *some_memory;
    int size_to_allocat e = ONE_K;
    int megs_obtained = 0;
    int ks_obtained = 0;

    while (megs_obtained< 724) {
    for (ks_obtained = 0; ks_obtained < 1024; ks_obtained++) {
    some_memory = (char *)malloc(size_t o_allocate);
    if (some_memory == NULL) exit(EXIT_FAILU RE);
    sprintf(some_me mory, "Hello World");
    }
    megs_obtained++ ;
    printf("Now allocated %d Megabytes\n", megs_obtained);
    }
    exit(EXIT_SUCCE SS);
    }
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: require this C malloc program analysis

    kernel.lover <cranium.2003@g mail.com> wrote:[color=blue]
    > I download this code from worx site with some of my
    > modification appear below. i want to ask why this program fails to run
    > after megs_obtained with 660 value?
    > Also as it concern with avail memory allocation, how more that
    > 256MB memory be given to this program?
    > I am using Fedora Core 1 with 256 physical RAM and 80GB HDD.[/color]
    [color=blue]
    > #include <unistd.h>[/color]

    That's a non-standard header and you don't need it for your program.
    [color=blue]
    > #include <stdlib.h>
    > #include <stdio.h>[/color]
    [color=blue]
    > #define ONE_K (1024)[/color]
    [color=blue]
    > int main() {
    > char *some_memory;
    > int size_to_allocat e = ONE_K;
    > int megs_obtained = 0;
    > int ks_obtained = 0;[/color]
    [color=blue]
    > while (megs_obtained< 724) {
    > for (ks_obtained = 0; ks_obtained < 1024; ks_obtained++) {
    > some_memory = (char *)malloc(size_t o_allocate);[/color]

    There's no good reason here to cast the return value of malloc() -
    it will only keep the compiler from complaining should you forget
    to include <stdlib.h>. And I hope you realize that this program is
    creating a huge memory leak since you don't keep a pointer to the
    memory you already got, so you won't be able to deallocate it...
    [color=blue]
    > if (some_memory == NULL) exit(EXIT_FAILU RE);
    > sprintf(some_me mory, "Hello World");
    > }
    > megs_obtained++ ;
    > printf("Now allocated %d Megabytes\n", megs_obtained);
    > }
    > exit(EXIT_SUCCE SS);
    > }[/color]

    Your question why it fails after a certain number of allocations
    can't be answered from a C point of view. malloc() must ultimately
    get the memory from the operating system and when it doesn't get
    more it returns NULL. Why the OS gives you that much memory but not
    more can only be answered in a newsgroup that discusses your system.
    Since you seem to be using some kind of UNIX (at least your include
    of <unistd.h> makes i look like that) you should ask in e.g.
    comp.unix.progr ammer.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • jacob navia

      #3
      Re: require this C malloc program analysis

      kernel.lover wrote:[color=blue]
      > hello,
      > I download this code from worx site with some of my
      > modification appear below. i want to ask why this program fails to run
      > after megs_obtained with 660 value?
      > Also as it concern with avail memory allocation, how more that
      > 256MB memory be given to this program?
      > I am using Fedora Core 1 with 256 physical RAM and 80GB HDD.
      > #include <unistd.h>
      > #include <stdlib.h>
      > #include <stdio.h>
      >
      > #define ONE_K (1024)
      >
      > int main() {
      > char *some_memory;
      > int size_to_allocat e = ONE_K;
      > int megs_obtained = 0;
      > int ks_obtained = 0;
      >
      > while (megs_obtained< 724) {
      > for (ks_obtained = 0; ks_obtained < 1024; ks_obtained++) {
      > some_memory = (char *)malloc(size_t o_allocate);
      > if (some_memory == NULL) exit(EXIT_FAILU RE);
      > sprintf(some_me mory, "Hello World");
      > }
      > megs_obtained++ ;
      > printf("Now allocated %d Megabytes\n", megs_obtained);
      > }
      > exit(EXIT_SUCCE SS);
      > }[/color]

      Maybe there is a per/user quota limit of 256MB for memory
      allocations?

      Comment

      • Jens.Toerring@physik.fu-berlin.de

        #4
        Re: require this C malloc program analysis

        jacob navia <jacob@jacob.re mcomp.fr> wrote:[color=blue]
        > kernel.lover wrote:[color=green]
        >> hello,
        >> I download this code from worx site with some of my
        >> modification appear below. i want to ask why this program fails to run
        >> after megs_obtained with 660 value?
        >> Also as it concern with avail memory allocation, how more that
        >> 256MB memory be given to this program?
        >> I am using Fedora Core 1 with 256 physical RAM and 80GB HDD.
        >> #include <unistd.h>
        >> #include <stdlib.h>
        >> #include <stdio.h>
        >>
        >> #define ONE_K (1024)
        >>
        >> int main() {
        >> char *some_memory;
        >> int size_to_allocat e = ONE_K;
        >> int megs_obtained = 0;
        >> int ks_obtained = 0;
        >>
        >> while (megs_obtained< 724) {
        >> for (ks_obtained = 0; ks_obtained < 1024; ks_obtained++) {
        >> some_memory = (char *)malloc(size_t o_allocate);
        >> if (some_memory == NULL) exit(EXIT_FAILU RE);
        >> sprintf(some_me mory, "Hello World");
        >> }
        >> megs_obtained++ ;
        >> printf("Now allocated %d Megabytes\n", megs_obtained);
        >> }
        >> exit(EXIT_SUCCE SS);
        >> }[/color][/color]
        [color=blue]
        > Maybe there is a per/user quota limit of 256MB for memory
        > allocations?[/color]

        Actually, he (or she?) seems to be getting at least 660 MB, unless I
        am misunderstandin g the OP. And there could be a whole lot of reasons
        why it stops there, starting (assuming UNIX) with per-process limits,
        user quotas, memory plus swap getting filled up (that's what I would
        place my bets on) and maybe several others. But without knowing which
        OS the OP is using as well as several system-specific parameters it's
        all just guesswork...
        Regards, Jens
        --
        \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
        \______________ ____________ http://www.toerring.de

        Comment

        • Keith Thompson

          #5
          Re: require this C malloc program analysis

          Jens.Toerring@p hysik.fu-berlin.de writes:
          [...][color=blue][color=green]
          >> kernel.lover wrote:[color=darkred]
          >>> hello,
          >>> I download this code from worx site with some of my
          >>> modification appear below. i want to ask why this program fails to run
          >>> after megs_obtained with 660 value?
          >>> Also as it concern with avail memory allocation, how more that
          >>> 256MB memory be given to this program?
          >>> I am using Fedora Core 1 with 256 physical RAM and 80GB HDD.[/color][/color][/color]
          [...][color=blue]
          > Actually, he (or she?) seems to be getting at least 660 MB, unless I
          > am misunderstandin g the OP. And there could be a whole lot of reasons
          > why it stops there, starting (assuming UNIX) with per-process limits,
          > user quotas, memory plus swap getting filled up (that's what I would
          > place my bets on) and maybe several others. But without knowing which
          > OS the OP is using as well as several system-specific parameters it's
          > all just guesswork...[/color]

          The OP told us he's using Fedora Core 1, which is a version of Red Hat
          Linux. A Linux or Unix newsgroup would be a good place to ask.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          Working...