TYFA
TYFA

Reputation: 313

Memory leaking with memory allocation

c++ <error: Cannot access memory at address 0x1> ​ I have an additional question about this question. The answerer said that the first one

des = new char[src.size() + 1];

will cause memory leaking since des is a local variable so he suggested another method right after.

char* toNormalWord(const std::string& src)
{
   char* des = new char[src.size() + 1];
   // stuff
   return des;
}

But I can't understand why the local variable will cause memory leaking and what is the difference between the first one and the second one. Isn’t the second one also using des as a local variable in the function? I thought the difference was just that the function receives des as a parameter or just creates themselves. I think I don't know something important but I don't know what that is...

Upvotes: 4

Views: 239

Answers (3)

Yankes
Yankes

Reputation: 2135

eerorika answer is correct, but can be expanded.

Then memory is allocated locally then this function responsibility is to deallocate it. If you return it then you push this responsibility on someone else and this is dangerous. You will have same problem like in your function but in other place:

char* toNormalWord(const std::string& src);

void processString(const std::string& src)
{
    char* des = toNormalWord(src);
    /* ... */
    if (c == '\n') throw new std::exception("invalid character!"); //memory leak of `des`!
    /* ... */
    return; //memory leak of `des`!
}

Now your memory is now local to other function and it should be free there.

Probably best way to avoid all this is use std::unique_ptr<char[]>:

std::unique_ptr<char[]> toNormalWord(const std::string& src)
{
    std::unique_ptr<char[]> des(new char[src.size() + 1]);
    /* ... */
    return des;
}

void processString(const std::string& src)
{
    std::unique_ptr<char[]> des = toNormalWord(src);
    /* ... */
    if (c == '\n') throw new std::exception("invalid character!"); //no memory leak!
    /* ... */
    return; //no memory leak!
}

with this compiler will always remember to free this memory.

In this specific case you can use std::string as suggested by barry. In some cases I even used std::vecotr for strings. All this depend on what usage of this "memory" is. std::string is best when you need do a lot of string operations like concatenation.

Upvotes: 0

eerorika
eerorika

Reputation: 238461

To understand the meaning of the sentence fragment "will just leak memory, since des is a local variable", one must understand the context. What was not said explicitly is that the value of the local variable was in no way copied elsewhere.

If the value is lost, then allocation is leaked.

what is the difference between the first one and the second one.

When the value assigned here: des = new char[src.size() + 1]; is not communicated to the outside of the function, the allocation will unconditionally leak at the end of the function.

When the value is returned, it can potentially be deleted later, thereby avoiding the leak.

Isn’t the second one also using des as a local variable in the function?

Yes. The difference is in whether its value is returned or not.

Upvotes: 2

Seil Choi
Seil Choi

Reputation: 54

first, des is local variable but it is pointer variable and you allocate (src.size() + 1) size memory so it allocated in heap memory in your process.

check this web site

http://www.cplusplus.com/doc/tutorial/dynamic/

Upvotes: 0

Related Questions