Malloc() and free() question

Lezard
Lezard used Ask the Experts™
on
Hi.

I had a question regarding malloc() and free().

If a function looks like this for example:

void function( void )
{
   char *ptr;

   ptr = malloc(100);

   strcpy(ptr, "Test pointer\n");

   free(ptr);
}

Now this function doesn't really do anything except allocates the memory and frees it but my question is, will this function use up more and more memory as it is called by the program or will it all be freed?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
This function does not "leak".  So while the SPECIFIC behavior of the system's memory allocation functions may cause the available memory to appear to decrease as this runs, the function is correct and should not cause problems over time.  What you will likely see is a stabilization of the memory consumption as a balance is struck between memory allocation and free'd block reclaimation.

Author

Commented:
Thank you.

That's exactly what I was wondering. =)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial