0

I have myself a function in C++ which is given a reference variable like so

public Draw(sfml::RenderWindow& window)
{
    window.draw(Sprite);
}

Basically I am passing my renderwindow variable as a reference to my drawObject which has a function known as Draw, this takes in the renderwindow reference and draws the image.

After the function has finished would the window reference variable be deleted and memory would be de-allocated or would a reference be forever made and slowly take up more memory as more and more renderwindow references are made?

Canvas
  • 5,779
  • 9
  • 55
  • 98

3 Answers3

1

Reference arguments (or variables for that matter) are not counted in C++. For practical purposes, references are just like normal pointers with different syntax and the addition that they always point to a valid value (or your program is ill-formed and invokes undefined behaviour). Also, references cannot be re-bound (so after initialization, a reference always refers to the same object).

Thus, like with normal pointers, references don’t force the object to live longer (with the notable exception of const type& binding a returned value). And like with pointers, you have to take care that the object the reference is refering to is still alive at all times the reference itself is alive.

Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
  • so once this function has completed the reference variable will be deleted from memory however my renderWindow will however stay alive (which is what I want) – Canvas Sep 24 '14 at 10:57
  • @Canvas There's absolutely no reason for the reference to even exist in memory. References can often be implemented without any memory allocation. References are not objects, so they don't have lifetimes, so you don't have to worry about that. The objects that they refer to are the only concern. – Joseph Mansfield Sep 24 '14 at 10:58
  • Awesomeness thank you Joseph, I have read up on pointers and references but found it quite hard to understand the memory management of them, so cheers for clearing that up – Canvas Sep 24 '14 at 10:59
1

References are not objects. They don't necessarily require any allocation of memory. They therefore don't have a lifetime for you to worry about.

The scope of the reference is your Draw function, which means that you won't be able to access the reference from outside of the function, but that's besides the point.

The only concern you need to have is about the lifetime of the object that your reference refers to. It's certainly possible for a reference to refer to an invalid object, because the lifetime of the object it was referring to has ended.

Pointers, however, are objects. If you had used a pointer instead, the pointer would have automatic storage duration because it is just a simple variable declaration. This means the pointer object's lifetime would last until the end of the function (the same as its scope). So even though pointers are objects, there's no extra concern (although pointers in general are usually not the best idea).

The allocation and deallocation of objects only becomes complex when you start dealing with new and delete, which create objects with dynamic storage duration. You should try to avoid this as much as possible. Note that in int* p = new int(5);, it is not the pointer that you need to worry about, but the int that the pointer points at. Note that it's impossible to dynamically allocate a reference (as it might have nothing to allocate!).

Community
  • 1
  • 1
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

All function arguments, whether they're built-in types, structures, pointers or references are cleaned up when the function returns.

This includes running destructors for types which have them. What it does not do is call delete or delete[] on any pointer - the compiler has no idea whether that makes sense. For instance, printf(const char*, ...) clearly should't try to delete a literal "Hello, World" argument.

So that's essentially the only way to leak memory via a function argument: use a pointer (but not a smart pointer), pass a new object, and forget to delete it in caller or callee.

MSalters
  • 173,980
  • 10
  • 155
  • 350