Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using malloc repeatedly on the same variable

Posted on 2004-09-06
12
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 375 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

670 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