469,307 Members | 2,181 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

malloc question again

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.

Jun 2 '06 #1
6 1768


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.

--
Er*********@sun.com

Jun 2 '06 #2

Jack wrote:
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.

Jun 2 '06 #3

Eric Sosman wrote:
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.


Assume that 5 blocks of ten bytes memory are allocated, which one does
p points to?
the last one?
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.


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

Jun 2 '06 #4
Jack schrieb:
Eric Sosman wrote:
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.


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().
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.


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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 2 '06 #5
uhhh yes...

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

Jack wrote:
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.


Jun 2 '06 #6
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
Jun 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Fatted | last post: by
1 post views Thread by Dawn Minnis | last post: by
24 posts views Thread by Hrv'uljak | last post: by
68 posts views Thread by James Dow Allen | last post: by
71 posts views Thread by desktop | last post: by
22 posts views Thread by ravi | last post: by
173 posts views Thread by Marty James | last post: by
6 posts views Thread by Peter Michaux | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.