Solved

using malloc repeatedly on the same variable

Posted on 2004-09-06
12
249 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

773 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