Free memory from pointer to pointer

Posted on 2011-04-21
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;


But I get an error if I try something like:

delete [] buffOut;
delete[] *buffOut;

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

Question by:atomicgs12
    LVL 31

    Accepted Solution

    >> *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

    LVL 31

    Expert Comment

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

    Author Comment

    so your saying it should be:
    *buff = new char[strBuff.length()+1];
    LVL 31

    Expert Comment

    >>    *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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    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.

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now