471,073 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

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_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);
}
Nov 14 '05 #1
4 1296
kernel.lover <cr**********@gmail.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
kernel.lover wrote:
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?

Nov 14 '05 #3
jacob navia <ja***@jacob.remcomp.fr> wrote:
kernel.lover wrote:
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?


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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
Je***********@physik.fu-berlin.de writes:
[...]
kernel.lover wrote:
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.
[...] 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.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

59 posts views Thread by Steve Zimmerman | last post: by
1 post views Thread by Jason | last post: by
31 posts views Thread by Caroline | last post: by
231 posts views Thread by Brian Blais | last post: by
54 posts views Thread by Neo | last post: by
15 posts views Thread by Martin Jørgensen | last post: by
71 posts views Thread by desktop | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.