require this C malloc program analysis

From: kernel.lover (cranium.2003_at_gmail.com)
Date: 03/10/05


Date: 10 Mar 2005 09:52:01 -0800

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_allocate = 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_to_allocate);
            if (some_memory == NULL) exit(EXIT_FAILURE);
            sprintf(some_memory, "Hello World");
        }
        megs_obtained++;
        printf("Now allocated %d Megabytes\n", megs_obtained);
    }
    exit(EXIT_SUCCESS);
}



Relevant Pages

  • Re: sizeof
    ... it's possible that malloc succeeds [ie: ... but the program fails later when it access this ... memory and the O/S discovers "oops, ... This whole scenario is for ...
    (comp.lang.c)
  • Re: sizeof
    ... it's possible that malloc succeeds [ie: ... but the program fails later when it access this ... memory and the O/S discovers "oops, ... four times over (real banks do not use the "Banker's Algorithm"). ...
    (comp.lang.c)
  • Re: require this C malloc program analysis
    ... jacob navia wrote: ... i want to ask why this program fails to run ... >> 256MB memory be given to this program? ... memory plus swap getting filled up (that's what I would ...
    (comp.lang.c)
  • Re: sizeof
    ... using any memory until something is written to it. ... you have at least 103 bits of address space, one could "allocate" ... but the program fails later when it access this ... This whole scenario is for ...
    (comp.lang.c)
  • Re: sizeof
    ... using any memory until something is written to it. ... you have at least 103 bits of address space, one could "allocate" ... but the program fails later when it access this ... the power fails. ...
    (comp.lang.c)