Solved

using malloc repeatedly on the same variable

Posted on 2004-09-06
12
244 Views
Last Modified: 2010-04-01
Hi,
Might be a silly question to ask...but I just want to be doubly sure.

I just came across a piece of code with the following line:-

buffer = (char*)malloc(8);

The code then goes on to use the buffer...and after a few more lines it again repeats the line:

buffer = (char*)malloc(8);

Apparently, the guy apparently uses this it to clear buffer....so that he can use it again.

At the end there is a single free(buffer) command.

I'd like to know if this is ok. If yes, then how does it work on the memory level? Does the same memory location get allocated over and over again? If not, then what happens to the previously allocated memory? Does it get "freed" automatically?

 


0
Comment
Question by:sandeep_th
12 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 11988603
Memory allocated by malloc must be released using free:

char *buffer;

while ( ... )
{
    buffer = (char*)malloc(8);

    // do something with buffer

    free(buffer);
}

This code is wrong:

char *buffer;

while ( ... )
{
    buffer = (char*)malloc(8);

    // do something with buffer
}

free(buffer);

It creates memory leaks, such program may crush after running for a long time.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 11988687
The author probably meant to use realloc, but it looks pretty sloppy. Best thing it to make it big enough for the worst cast and leave it intact.
0
 
LVL 30

Expert Comment

by:Axter
ID: 11991337
You usally want to use malloc/new when you're not sure of the memory size at compile time.
However, in this case, a fix number is being used at compile time, so it makes no sense to use malloc at all.

Consider using a static size variable instead.

char FixSizeBuffer[8];
buffer = FixSizeBuffer;

Then you don't have to worry about calling free, or about creating memory leaks.
0
 

Expert Comment

by:s_senthil_kumar
ID: 12029581
That code will leak memory for sure. Every call to malloc should be paired with a free and if it isn't, you'll leak memory for every free that has not been done.
0
 
LVL 1

Expert Comment

by:bsnh99
ID: 12060721
What everyone else has said is true, but beware of the case where the buffer pointer is passed to a function where free is called. Also, in some more complex code, the pointer values may be stored in a data structure and freed later. With the description you gave, we can't really know for sure.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:sandeep_th
ID: 12063341
I agree with all of you(Axter et al ) completeley.....but pray tell me...why doesn't a memory leak finder tool find a leak in this piece of code. I am using efence(the tool that comes with SuSE linux).
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12305640
None of us responded to the author's last question. Give it to AlexFM with a B.
0
 

Author Comment

by:sandeep_th
ID: 12306433
Fair Enough.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 12307312
Hi sandeep_th

I would really appreciate if you could close the question yourself.
It really helps that the asker closes the question as he better than anyone else knows if the comments provided were the answer to the question or not.

Thanks in advance

Tincho
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12307487
He did, Tincho. 8-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now