questions? wrote:
I am using malloc to allocation memory for a group of structures. I am
not using a whole lot of memory.
But my program can run without a problem on a machine with 2GB RAM, but
with a machine less than 1GB memory, it fails to allocate memory.
What are the reasons the system cannot allocate memory? How can I fix
this problem?
( when the program starts, I am only using about 40MB memory which is
not a whole lot)
Please help me if you have any ideas
My guess is that there is an error in your code.
Perhaps you've miscalculated and did something like
the following:
int sizes[] = {10, 20, 30};
....
ptr = malloc (sizes[4]); /* using garbage value, UB, etc */
Or maybe you did something like this
int length;
....
ptr = malloc (length); /* length uninitialised */
Or you may have even done this
int length = 0;
....
ptr = malloc (length); /* implementation defined,
can return NULL */
Whichever it is, I seriously doubt that any small
program that manipulates a group of structures
can legitimately exhaust memory on a 1G ram
implementation. If you are dealing with such large
amounts of data you are either:
a) reading it from disk and therefore you know how
large it is on disk (is it really supposed to be
a gig?) and should not be surprised when you run
out of memory.
b) you are generating the data yourself, which should
take a tremendous amount of time for non-trivial
algorithms to fill up a gig.
So, please post the smallest compilable code that
causes this problem and perhaps we can spot the
problem.
If you are generating the data from an algorithm,
a short description of the algorithm would be nice
too.
goose,