2

In this question the OP explores the idea of a function which returns a reference on a dynamically created object.

intArray& createArray()
{
    intArray *arr = new intArray(10000, 0);
    return(*arr);
}

The answers are that even dough this will compile, it will confuse the programmer who is using the function. He will be confused because it is expected that when a function returns a reference, the 'user' of that reference isn't responsible for its memory management.

I have noticed that there are lots of API methods which return pointers. Does that imply that I'm responsible for clearing (deleting the object and setting the pointer to NULL) them once they aren't needed any more?

Community
  • 1
  • 1
TheMeaningfulEngineer
  • 15,679
  • 27
  • 85
  • 143
  • You need to check the documentation for that interface. For yourself, write better interfaces! – BoBTFish Oct 29 '13 at 10:14
  • it depending on the api. – user1810087 Oct 29 '13 at 10:14
  • It will definitely be described in the documentation unless it's under-documented. – yzn-pku Oct 29 '13 at 10:18
  • Generally speaking I would look at the function name as well as return type, to determine ( to guess actually ) if I need to perform clean up. APIs should typically document where ownership lies. If a function is named Create...() I would think that I have to perform cleanup, but wouldn't take it for granted without collaborating evidence, such as source code or documentation. – Kindread Oct 29 '13 at 10:19

4 Answers4

2

Does that imply that I'm responsible for clearing [...] them once they aren't needed any more?

Often, but not always. To be sure, you need to check the the documentation for the particular API that you are using.

In your own APIs you should give preference to smart pointers over raw pointers. This makes the intent clear and simplifies many memory-management issues.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Why would a function return a pointer, and not a reference, if the 'user' of the function isn't responsible for the returned object? – TheMeaningfulEngineer Oct 29 '13 at 10:28
  • 1
    @Alan: There could be many reasons. The first two that come to mind are: (1) A pointer could be NULL (a reference cannot). (2) The programmer might habitually be using pointers where references would arguably be more suitable (perhaps because they come from a C background). – NPE Oct 29 '13 at 10:33
1

Not usually, but it depends. Typically, factory functions will return pointers which you have to delete (but there are exceptions to this as well); other functions will return pointers which you shouldn't delete.

One convention is that functions which return pointers which you should delete should return std::auto_ptr (or std::unique_ptr in C++11). At one time, this was highly recommended, but it doesn't seem to have been widely adopted.

Other than such conventions, you generally will have to read the library documentation.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
1

The pointer itself has no implication of ownership, and pointers are often used to refer to objects that they aren't supposed to own. If you want to manage resources by juggling pointers, then you'll need to document which pointers are supposed to own them. Then you just need to hope the user reads and follows the documentation, and that future maintainers don't let the code and the documentation drift apart.

To explicitly state and enforce ownership, return a smart pointer or container. These have the bonus of using RAII to fix the exception-safety issues that are almost inevitable otherwise. In this case, since you're returning a dynamic array, std::vector<int> or std::unique_ptr<int[]> would be most appropriate.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

It depends on the API. See the documentation.

The C strdup function returns a pointer you should free when done with it (the API alocated it for you) The C strcpy function returns a pointer that it did NOT allocate.

manuell
  • 7,528
  • 5
  • 31
  • 58
  • I understand it can be undefined in C with the absence of references. I was aiming for c++ – TheMeaningfulEngineer Oct 29 '13 at 10:16
  • @Alan You asked about "function returns a pointer". I stand with what I and others said: "It depends, see the docs". You may want to clarify your question. – manuell Oct 29 '13 at 10:20
  • I didn't specify c++ explicitly since it's in the tags an c isn't. I don't know the recommended practice for additional explicit definition of the question scope together with tags. If you do, please post a reference of the rule/best practice. – TheMeaningfulEngineer Oct 29 '13 at 10:25