malloc question again

J

Jack

In the following code:

char *p;

while(1){

p = malloc(10*sizeof(*p));

//Do something with p

}

The while( ) loop keeps allocate memory to p. What will happen?

Should I do as below?

char *p;

while(1){

p = malloc(10*sizeof(*p));

//Do something with p

free(p);
}

Thanks a lot.
 
E

Eric Sosman

Jack wrote On 06/02/06 12:54,:
In the following code:

char *p;

while(1){

p = malloc(10*sizeof(*p));

//Do something with p

}

The while( ) loop keeps allocate memory to p. What will happen?

Each iteration of the loop will call malloc() and
request another ten bytes. Unless the "do something"
code free()s some of the allocated memory, malloc()
will eventually exhaust all the available memory and
return NULL to indicate that it couldn't honor the
request.
Should I do as below?

char *p;

while(1){

p = malloc(10*sizeof(*p));

//Do something with p

free(p);
}

That is one thing you could do. Another would be

p = malloc(10 * sizeof *p;
if (p == NULL) ...
while (1) {
//Do something with p
}
free (p); // assume a `break'

Still another might be to put the p values into a data
structure of some kind -- linked list, tree, whatever --
that will "remember" the values so you can access the
allocated memory later.
 
S

spibou

Jack said:
In the following code:

char *p;

while(1){

p = malloc(10*sizeof(*p));

//Do something with p

}

The while( ) loop keeps allocate memory to p. What will happen?

Depends on the operating system. malloc() may start returning NULL
from some point onwards , or your programme will crash or terminated
by the operating system and it's even possible that the operating
system
will get confused and terminate other programmes. I have heard the last
possibility happening in Linux.



Should I do as below?

char *p;

while(1){

p = malloc(10*sizeof(*p));

//Do something with p

free(p);
}

Yes , you can do that. Of course if you know in advance that you're
going
to need space for 10 char's you don't need to use malloc() at all ;
just declare
an array of size 10.
 
J

Jack

Eric said:
Jack wrote On 06/02/06 12:54,:

Each iteration of the loop will call malloc() and
request another ten bytes. Unless the "do something"
code free()s some of the allocated memory, malloc()
will eventually exhaust all the available memory and
return NULL to indicate that it couldn't honor the
request.

Assume that 5 blocks of ten bytes memory are allocated, which one does
p points to?
the last one?
That is one thing you could do. Another would be

p = malloc(10 * sizeof *p;
if (p == NULL) ...
while (1) {
//Do something with p
}
free (p); // assume a `break'

Still another might be to put the p values into a data
structure of some kind -- linked list, tree, whatever --
that will "remember" the values so you can access the
allocated memory later.

If each time I have to allocate a different size of memory to p, such
as:

p = malloc(N * sizeof(*p)); //N is a variable

then I have to put the malloc into the while loop, right?

Thanks.

Jack
 
M

Michael Mair

Jack said:
Assume that 5 blocks of ten bytes memory are allocated, which one does
p points to?
the last one?

Please be precise in your questions -- I am not sure whether you
are asking for the value of p or the relative position of allocated
storage...

p at "// Do something ..." obviously either is a null pointer
(perfectly possible) or points to at least 10 bytes of
allocated storage.
As long as p does not become a null pointer, p points to the storage
returned by the latest call to malloc().
If each time I have to allocate a different size of memory to p, such
as:

p = malloc(N * sizeof(*p)); //N is a variable

then I have to put the malloc into the while loop, right?

Not necessarily.
It may suffice to know the value range N can have during a
run of the programme and allocate sufficiently much storage
before the loop.
Or have an array of the respective size.
Another thing would be to have
size_t size = 0;
char *p = NULL;


p = malloc(INIT_SIZE * sizeof *p);
if (p == NULL) {
/* Your error handling here */
}
size = INIT_SIZE;

while (1) {
....
if (N > size) {
char *temp = realloc(p, N);
if (temp == NULL) {
/* Your error handling here; p is still valid */
}
p = temp;
size = N;
}
....
free(p);

If you explain what exactly you have in mind, we may be able
to help you better.


Cheers
Michael
 
V

v_temp

uhhh yes...

if you reset your pointer to a different area of memory you are
creating the definition of a memory leak...
 
T

Tomás

Jack posted:

The while( ) loop keeps allocate memory to p. What will happen?


Here's a dynamic allocation exhaustion test for you:


(Unchecked code, likely to contain an error or two:)


typedef struct MemAllocInfo {
struct MemAllocInfo *previous;
} MemAllocInfo;


#include <stdlib.h>

int main(void)
{
MemAllocInfo info = {0};

MemAllocInfo *p_info = &info;

for (;;)
{
MemAllocInfo *tempptr = malloc( sizeof(*tempptr) );

if (!tempptr) break;

tempptr->previous = p_info;

p_info = tempptr;
}


do
{
MemAllocInfo *tempptr = p_info.previous;

free(p_info);

p_info = tempptr;
}
while( p_info );
}


(That code my call "free" on the stack-allocated object...)


-Tomás
 

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

Similar Threads


Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top