0

How to manage memory in function, which returns dynamically allocated variable? What happens with buffer, when function returns?

char * getStr(){

    char * buffer = new char[12];
    sprintf_s(buffer, 12 , "abcdef");

    return buffer;
}
  • 3
    in `c`, AFAIK , `new` is not there. – Sourav Ghosh Dec 10 '14 at 14:00
  • 3
    Please tag your question as either C or C++, not both. In C, you use `malloc` for dynamic memory, while in C++ you use `new`. – Colonel Thirty Two Dec 10 '14 at 14:02
  • 4
    Maybe its time to learn std::string instead of using c strings in c++. – drescherjm Dec 10 '14 at 14:03
  • I've changed my queston. So you say that it's incorrect to use char in c++? I know what is std::string. But I always was interested what happens with buffer in my case. – Vladyslav Yefremov Dec 10 '14 at 14:06
  • 2
    People are answering your question well, but in c++ we try to avoid passing suff like this. It is better to return a std::string (which owns and manages its own memory) than to trigger the above situation. – IdeaHat Dec 10 '14 at 14:09

5 Answers5

2

buffer stays allocated, but luckily you're returning the pointer.

You must delete[] that pointer at some point, else you'll leak memory.

Notice how I've used []: that's important. This balances your allocation of an array of chars. (Conceptually the runtime stores the length of an array allocated with new something[], and delete[] informs the runtime to free the correct number of elements.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

When function returns, your buffer still exists. There is nothing like embedded memory manager, you MUST free all the memory you allocated manualy. That is about C.

In C++ standart library, there is objects called smart pointers. With the pressence of exceptions, they are totally recommended to use. There is fine answer on SO about them: What is a smart pointer and when should I use one?

Community
  • 1
  • 1
Vasilly.Prokopyev
  • 856
  • 1
  • 10
  • 24
0

Nothing.

The address which is stored in *buffer will be returned to the caller and it is up to the caller to delete the memory again.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

buffer is a pointer that means it addresses a point in memory.

new will allocate a bloc of memory, for the command new char[12] it will allocate 12 chars worth of memory.

new returns the address of the memory and that is assigned to your pointer buffer.

Note that because you only have a pointer to the memory, you need to clean that up before buffer goes out of scope. If the address contained in buffer buffer goes out of scope you will have "leaked" memory. Because it cannot be reclaimed. You clean up the memory allocated by new char[12] with the command: delete[].

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0

Thank to Bathsheba for his answer.

My vision of a solution to the problem:

char * getStr(){

    char * buffer = new char[12];
    sprintf_s(buffer, 12 , "abcdef");

    return buffer;
}

int _tmain(int argc, _TCHAR* argv[]){

    char * p = getStr();

    std::cout << p << std::endl;

    delete[] p;
    return 0;
}
Community
  • 1
  • 1