[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Free memory from pointer to pointer

Posted on 2011-04-21
4
Medium Priority
?
397 Views
Last Modified: 2013-12-27
I have an application where I have a function that I want to return some data from a file and the data retrieved from the file can be different so the memory size of the variable has to be dynamic.

What I have is a function like

GetData(char **buff)
{
      // where I do something like
      // read file and fill an std:string object up

      std::string strBuff;
      While not end of file
            strBuff.append(line from file);

      // here I create new memory and pass it back from to the caller
      *buff = new char[strBuff.length()];

      strcpy(*buff, strBuff.c_str());
      
}

When I call the function it is something like:

char *buffOut = NULL;

GetData(&buffOut);

But I get an error if I try something like:

delete [] buffOut;
Or
delete[] *buffOut;

What is the correct way to delete this memory I have allocated in the GetData function?

Thanks
0
Comment
Question by:atomicgs12
  • 3
4 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 35445075
>> *buff = new char[strBuff.length()];
    -- does not provide enough space for the null byte
>> strcpy(*buff, strBuff.c_str());
    -- overwriting the allocated buffer - resulting in heap corruption

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35445079
>> What is the correct way to delete this memory I have allocated in the GetData function?
This should work after fixing GetData
>>     delete [] buffOut;
0
 

Author Comment

by:atomicgs12
ID: 35445555
so your saying it should be:
*buff = new char[strBuff.length()+1];
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35445578
>>    *buff = new char[strBuff.length()+1];
    Yes, now you allocate enough room to include the terminating null byte. Now when you do your strcpy(*buff, strBuff.c_str()), where you are copying the string length +  the null byte, you are copying within the allocated region.
0

Featured Post

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!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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