How Can You Tell When A Class Object Disposes?

I have been using classes for quite some time now and just basic classes get be How can you tell when an object doesn't exist anymore?


For instance take the code for this very simple class on this site

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// classes example
#include <iostream>
using namespace std;

class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
};

void CRectangle::set_values (int a, int b) {
  x = a;
  y = b;
}

int main () {
  CRectangle rect;
  rect.set_values (3,4);
  cout << "area: " << rect.area();
  return 0;
}


The object instance rect how do you know when that no longer exists anymore and you can't reach it again? Many times I find myself reusing like rect.set_values( again and again and then calling the objects functions again? Is this bad?

What am I missing and what should I know? this just confuses me a bit?

Thanks in advance.
The object no longer exists when it goes out of scope. If you can refer to it by name and you don't get a compiler error, its still in scope.

In this case, "rect" is in the scope of main(), and so it goes out of scope at line 21, when the program ends.
Although you do need to remember that objects allocated with new don't get deleted automatically, even if the pointer to the memory does:
1
2
3
4
5
6
7
void NotSafe()
{
int *pointer = new int;
}
//At the end of this function pointer will be destroyed, but the memory will still exist.
//That's a memory leak, since if you keep calling this function, you'll allocated more
//and more memory which never gets deleted. 
So is it better to use pointers so you can control when it goes out of scope? That I knew? Because in a sense if you declare an object in the main like
CRectangle rect; won't that be hording memory for the remainder of the rest of the program?

It would be better to construct that object within a function so it would destruct itself when you don't want it anymore; or is this not correct?

In another words there could be another function that creates the object like

1
2
3
4
5
6
void AddUsingClass(int a, int b)
{
  CRectangle rect;
rect.set_values(a,b);
cout << rect.area() << endl;
}


Then rect would destruct after the function. Wouldn't this save memory or no? Does this matter that much?
So is it better to use pointers so you can control when it goes out of scope?

no

It would be better to construct that object within a function so it would destruct itself when you don't want it anymore;

yes

You should work with the language, not against it. Let the scope destroy your objects, let it provide memory management and exception guarantees. The less you have to do, the less room you have for error.
Don't be too worried about memory usage unless you know you're going to be allocating millions of objects. I doubt your rect class takes up more than a few bytes.

If you only need the object inside that function, then yeah, you can just construct it there. But remember that constructing objects can take time, especially for containers which need to dynamically allocate memory. It's generally good practice to declare an object just before you need it.

Finally, no it's not better to use pointers. C++ is very clear about when objects go out of scope, so you don't need to worry about accidentally losing some object. Pointers would be when this problem might emerge. I'm just warning you to remember that you can lose a pointer, but still have the (now invisible) memory allocated.

If you need variable amounts of space, it's (almost) always better to use a container class, the most basic being vector, or your own wrapper around the new and delete calls.
After the destructor is called the object is nore more. It goes out of scope when the memory it is located in is no longer be operated on by the cpu. If you use dynamic memory (pointers) the object is gone when it is delete(insert alias name here).
Ex: int* p = new int[40];//allocate 40 integers (40 dwords = 40 * 2 * 8 = 40 * 16 = x bits)
lets say you have an object called sammy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class sammy{
private:

some_var foo;

public:


};

int main()
{

sammy* s = new sammy[10];//ten sammies of size(foo) + extra for any virtual methods, destructors.

delete [] s;//no more sammy

return 0;
}


in general convention an object is gone when it goes out of scope. lets say we have a method (function) that uses sammy (other than main(...)).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class sammy{
private:

some_var foo;

public:

void doSomething(){

   std::cout << "my name is sammy" << std::endl;
}
};
//Warning will cause memory leak:
void foo1()
{
    sammy* s = new sammy[10];
    
   for(int i = 0; i < 10; i++)//for every sammy out there :P
{
  s[i].doSomething();
}
//oops forgot to "delete" s;  //s is still allocated in memory, now we have memory leak
//delete[]s;//FOR multiple sammy,//explicitly delete 10 sammy "a pointer to 10 sammy objects"
//::operator delete(s); FOR new sammy; //only one sammy
//implicitly return; (void identifier)
}

void foo2()
{
  sammy s;//give the compiler sole responsibily to make sure that theres is one sammy
                //in this method

 //implicitly return;//s (sammy) goes out of scope and is unreachable from "main" method
}

int main()
{
  foo1();//call foo1 which will allocate 10 sammy
  foo2();//call foo2  

return 0;//exit to kernel
}
Last edited on
^You need to use delete [] s; since you allocated with new [].
true, thanks for catching that ill edit it.^
Thanks guys this has been really helpful. Hopefully someone can answer one more question so Cubbi said it's better to user functions even with object instances letting the computer do the works etc. I agree but I've been using a lot more complicated classes then this rect one I'm just using this for example but I haven't yet been close to bogging down any computer etc. So worrying about memory too much is probably silly.

That said using the rect class since

void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}

Is set later when you create an object like rect; in the example does the set_values part of this destruct in my example before the return 0;

i.e. before the program ends since this function was declared outside of the class in terms of what it does or because it's also in the class

void set_values (int,int);

Maybe it isn't destroyed before the program ends?

Thanks guys otherwise I get this.
Topic archived. No new replies allowed.