-1

So I have this function:

char * func()
{
char * temp = new char[length]; // Length is defined elsewhere
/*
Do some stuff with temp
*/
return temp
}

My problem is I'm not sure whether or not this leaks memory. If so, I don't know how to make it not do that. I thought I could delete it after return, but how would I do that? (Also assuming i can't just delete it outside the function) This function is part of a class. This function's purpose is to return an internal value of the class. A user would use this function. i find it too much to ask that the user deletes the value afterwards. Thanks!

chboo1
  • 63
  • 9
  • 2
    No, it doesn't leak as long as you `delete[]` result somewhere. – Yksisarvinen Apr 18 '22 at 11:56
  • As I said (sorry if i was unclear) i can't delete it elsewhere. – chboo1 Apr 18 '22 at 11:58
  • Why can't you `delete[]` it after return? – ChrisMM Apr 18 '22 at 11:59
  • 5
    Anyway, using `std::string` properly bypasses this issue entirely. – Paul Sanders Apr 18 '22 at 11:59
  • 1
    Preferrably return an object owning the data; `std::string`, `std::vector` or `std::unique_ptr` would do the trick... – fabian Apr 18 '22 at 12:00
  • One option is to document that whatever code calls this function should eventually `delete[]` it; if code doesn't, that's a leak. But in modern C++ it's usually best to avoid `new` and `delete` entirely: return a `std::string` or `std::vector` or even a `std::unique_ptr`. – aschepler Apr 18 '22 at 12:00
  • This function is part of a class, this function should return some sort of internal value in the class, so i find it too much to ask the user to delete the value afterwards – chboo1 Apr 18 '22 at 12:01
  • Edited my question, hope it's more clear – chboo1 Apr 18 '22 at 12:08
  • @chboo1 _"this function should return some sort of internal value in the class"_ well, then you're doing it wrong, if it should be an internal class member, you missed to assign it from `temp`. – πάντα ῥεῖ Apr 18 '22 at 12:09
  • 4
    ***My problem is I'm not sure whether or not this leaks memory*** It will leak memory if delete[] is not called eventually on the pointer you return. You probably want to return a `std::string` instead of `char*`. This would completely eliminate the problem and simplify the code. – drescherjm Apr 18 '22 at 12:09

2 Answers2

1

My problem is I'm not sure whether or not this leaks memory.

This doesn't leak memory, because the pointer to the allocation is returned and thus the ownership is transferred to the caller. If the caller loses the returned pointer value before deleting it, then the caller has leaked memory.

That said, this is a bad design. You should avoid owning bare pointers. Instead, in this case I recommend returning a vector. This way the vector template will take care of releasing the allocated memory:

std::vector<char> func()
{
    std::vector<char>(length) temp;
    /*
    Do some stuff with temp
    */
    return temp;
}

Alternatively, you could consider using std::string if the array represents text.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Not exactly what i was looking for, but it did help me come up with a solution: I make a member of my class be that value, then i would modify that value, return it, and delete it when my class' destructor will be called – chboo1 Apr 18 '22 at 12:09
  • @chboo1 I recommed making the member a `std::vector`/`std::string` instead. Bare owning pointers are bad as members as well. – eerorika Apr 18 '22 at 12:12
  • I'm trying to get around std stuff like std::string, it's a personal challenge, but yes that is great advise thx – chboo1 Apr 18 '22 at 12:14
  • 1
    @chboo1 In that case I recommed starting by learning about rule of 5. Otherwise you will have bugs. – eerorika Apr 18 '22 at 12:15
  • i know i am a masochist, yes. but that is inapropriate – chboo1 Apr 18 '22 at 12:17
0

Making an answer for unique_ptr as it is the closest to what was done in your sample.

auto func()
{
 auto temp = std::make_unique<char[]>(length); // Length is defined elsewhere
 /*
    Do some stuff with temp
    */
 temp[0] = 12;
 return temp;
}

As memory is managed by smart pointer, there is no leak and is it self documented of who is the owner of the memory hunk: the one who owns the unique_ptr

OznOg
  • 4,440
  • 2
  • 26
  • 35