require this C malloc program analysis

K

kernel.lover

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);
}
 
J

Jens.Toerring

kernel.lover said:
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>

That's a non-standard header and you don't need it for your program.
#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);

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...
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);
}

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.programmer.
Regards, Jens
 
J

jacob navia

kernel.lover said:
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);
}

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

Jens.Toerring

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

Actually, he (or she?) seems to be getting at least 660 MB, unless I
am misunderstanding 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
 
K

Keith Thompson

[...]
Actually, he (or she?) seems to be getting at least 660 MB, unless I
am misunderstanding 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...

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top