-1

The code below is a toy example of something I'm trying to do that might be misguided. I believe this is leaking memory since I'm using new but not deleting. Is that correct, and if so, is there any way to delete the leaking stuff within the main method? Or is it inaccessible at that point?

#include <iostream>
using namespace std;

int* f(){
    int* x = new int;
    *x = 4;
    return x;
};

int main(){
    cout << *f();
    return 0;
}
bart
  • 13
  • 1
  • i would look up what the heap and the stack are. – Daniel A. White Feb 26 '20 at 03:55
  • @DanielA.White yeah, i kind of know what they are. do you mean so that i can learn why i shouldnt use new here? im aware its a dumb use of the heap. its a minimal example of a case where i thought it might not be dumb – bart Feb 26 '20 at 03:57
  • 2
    Correct, you're leaking memory. For a full discussion of proper memory management, including the many resources in the C++ library that will automatically delete memory after it is no longer used, see your C++ book. Unfortunately, stackoverflow.com is not really a replacement for a C++ book, so you should really direct your attention to reading yours', and practicing the sample programs in there. This is the only way you will learn the material correctly. – Sam Varshavchik Feb 26 '20 at 03:57
  • @SamVarshavchik ok, fair enough. thanks. – bart Feb 26 '20 at 03:59
  • So long as you have a pointer to the allocation, you can release it. Take great care to not delete it twice, for example if you wind up with two pointers to the same allocation for one reason or another. The really tricky part is when you get a pointer to something and you don't know where it came from how should you, or should you at all, delete it? Recommended reading: [What is meant by Resource Acquisition is Initialization (RAII)?](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii). RAII leads to a whole bunch of other concepts. – user4581301 Feb 26 '20 at 04:00
  • @user4581301 thanks for the link; i will read. my quandary was that calling f creates a pointer to the heap, but i dont know how to "refer" to the pointer after f was called. i can't write "delete x", and i feel like calling f again just creates another pointer to another spot on the heap... anyway, thanks for your help. – bart Feb 26 '20 at 04:08
  • Jeremy just dropped an answer that is a longer-worded version of what I was about to put in a comment. – user4581301 Feb 26 '20 at 04:12

3 Answers3

4

The trick would be to capture the returned pointer-value into a variable, so that you can refer to it more than once inside main(). Like this:

int main(){
   int * ptr = f();
   cout << *ptr;
   delete ptr;
   return 0;
}

(Side note: it's preferred to return an int by-value instead, but in cases where you are required to dynamically allocate the object you are returning (e.g. when you want to use run-time polymorphism), you should return the allocated object via a smart-pointer rather than a raw/C-style pointer. That way you won't have to remember to delete the returned object, and therefore you can't forget to do so and cause a memory leak)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • ah of course. thanks so much. the reason this is the best stackexchange site: a post deservedly gets three downvotes, but then also gets a perfect answer. – bart Feb 26 '20 at 04:13
0

We have to store the return variable into a integer-type pointer so that it can be accessed into the main.

int main()
{
    int * ptr = f();
    cout << *ptr;
    delete ptr;
    return 0;
}
0

You can do even better: smart pointers!

#include <iostream>
#include <memory>

int* f(){
    int* x = new int;
    *x = 4;
    return x;
};

int main(){
    std::cout << *std::unique_ptr<int>(f());
    return 0;
}  // No memory leak!

Really, you should try to not use new/delete in your own code unless you have to.

Jan Hošek
  • 187
  • 1
  • 9
  • Perhaps have `f()` return a `std::unique_ptr` instead of an `int*`, that way you don't have to rely on the calling code not to leak the returned object? – Jeremy Friesner Feb 26 '20 at 17:20
  • Sometimes you don't have a choice about what API you have to use. Of course returning `unique_ptr` is better but more often than you'd like you come across something returning a raw pointer. – Jan Hošek Feb 27 '20 at 06:56