Solved

using malloc repeatedly on the same variable

Posted on 2004-09-06
12
247 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 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

23 Experts available now in Live!

Get 1:1 Help Now