Solved

using malloc repeatedly on the same variable

Posted on 2004-09-06
12
255 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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.
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.

630 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