Using new several times without delete

Dear all,

I would like to know if I'm doing something wrong in C++.
I have a class with a pointer and a member function that allocates the pointer using the new operator.

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
class MyClass 
{
private:
   mytype* pointer;
public:
   void myfunction()
   {
      pointer = new mytype(); 
      // I never calls: delete pointer; pointer = 0;
      // What happens with memory? 
      // pointer is only reallocated, without collateral effects?
   }
   
   void dosomething()
   {
      pointer->DoSomething();  
   }
};

int main()
{
   MyClass a;
   for ( int i = 0 ; i < 1000 ; i++ )
   {
      a.myfunction();
      a.dosomething();
   }
   return 0;
}


Is it correct? I never call the delete operator, is that dangerous?
If I call myfunction several times I will reallocate memory and my function dosomething will operate with the last allocated pointer, is it wright, is there some problem of memory leak?

Thank you very much any kind of explanation!
closed account (Lv0f92yv)
C++ is not a garbage collection language, what this means is that when you reallocate memory and assign it to pointer 'pointer', the other memory is left useless - it cannot be reclaimed by your program unless you de-allocate it (using delete). So technically yes - there would be memory leak.

Would it be a problem? Probably not unless this code was running constantly (the pointer was constantly being assigned new memory) over a long period of time, or if that line of code was executed hundreds of thousands or millions of times. I suppose it would also depend on how much memory is available for your program.

Get in the habit of de-allocating memory - maybe in this case, in the destructor.
Last edited on
Consider these changes:
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
class MyClass 
{
private:
   mytype* pointer;
public:
   MyClass() // default (single-argument) constructor
   {
      // create mytype instance on the heap
      pointer = new mytype(); 
   }

   ~MyClass() // destructor
   {
      // properly destroy the instance and return its memory
      delete pointer; 
   }
   
   void dosomething()
   {
      pointer->DoSomething();  
   }
};

int main()
{
   MyClass a;              // create a local instance of MyClass on the stack, calling the default constructor
   for ( int i = 0 ; i < 1000 ; i++ )
   {
      a.dosomething();
   }
   return 0;
}                          // <- at the end of the block (or in the event of an exception) the local
                           // variable will be properly destroyed (calling its destructor) and returning its memory 


Using the constructor to acquire a resource (memory, in this case), a destructor to release it, and using a local variable to control the life of the variable is an approach called Resource Acquisition Is Initialization (RAII). That is how I would implement your example--letting MyClass manage or own the allocated object.

Another nice alternative would be to store the instance in a reference-counted pointer, such as boost::shared_ptr. Then the delete would be performed when a is destroyed because nothing points to the object any more (the reference count equals zero).

Just something to think about.
Last edited on
Great explanations!
Desh, thank you very much for your explanation.
Moorecm, thanks for the example.
I have a question about this that someone might be able to help me out with. What is with the "private" and "public" lines? I have never seen them before and am curious as to what they do.
They're called access specifiers and are extremely important when using a class.

http://cplusplus.com/doc/tutorial/classes/
Topic archived. No new replies allowed.