In the following program, why there is no delete in the destructor? is it because there is no pointer involved, so no memory needs to be released?
How about the memory allocated when constructing M,P,S,N, the memory will be release automatically when the program ends?

Thanks

#include<iostream.h>
class point
{
public:
point(int x,int y) {X=x,Y=y;cout<<"Constructor called!"<<endl;}
point(point &p);
~point() {cout<<"Destructor called"<<endl;}
int getX(){return X;}
int getY(){return Y;}
private:
int X,Y;
};
point::point(point &p)
{
X=p.X;
Y=p.Y;
cout<<"Copy-initialization Constructor called!"<<endl;
}
point fun(point A)
{
cout<<"The function of \'fun\' is called!"<<endl;
int x=A.getX()+5;
int y=A.getY()+10;
point R(x,y);
return R;
}
void main()
{
point M(6,7),P(0,0),S(0,0);
point N(M);
P=fun(N);
S=M;
cout<<"P="<<P.getX()<<","<<P.getY()<<endl;
cout<<"S="<<S.getX()<<","<<S.getY()<<endl;
}

Recommended Answers

All 3 Replies

delete is the keyword for dynamic de-allocation of memory. I see no new creating a dynamic allocation, so it is not needed.

It is used with pointers, and I don't even see any pointers.

do you mean if there is pointer as the class's internal data member, then I should use delete in the destructor, even there is no new (dynamic allocation)?

How about the memory allocated when constructing M,P,S,N, the memory will be release automatically when the program ends?

Correct, it will release automatically. These are not dynamic allocations. They are already known at compile time, so the compiler has already allocated memory for them.

do you mean if there is pointer as the class's internal data member, then I should use delete in the destructor, even there is no new (dynamic allocation)?

Not necessarily, it depends on how the pointer is created/initialized and used.

Pointers can be used 2 different ways:

//a "non-dynamic" pointer example
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main() {
  srand((unsigned)time(0));
  int anInt = 0;  //declare an integer (skipped in dynamic allocation)
  int *pInt = NULL;  //declare an integer pointer
  pInt = &anInt;  //assign anInt's address to pInt (automatic in dynamic allocation)

  anInt = rand();
  cout << "The value stored in integer anInt is " << anInt << endl;
  /* ^ --- this is not possible with dynamic allocation (i.e. new/delete) */
  cout << "The address of anInt is " << pInt << " the value is " << *pInt << endl;
  /* ^ --- this, however, is the only way to get to the value */

  pInt = NULL;  //reset the pointer
  cout << "The value of anInt is still " << anInt << endl;
  cin.get();
  return 0;
}

^---- is functionally similar (not identical/equivalent) to ----V

//a "dynamic" pointer example
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int main() {
  srand((unsigned)time(0));
  int *pInt = NULL;  //declare an integer pointer
  pInt = new int;  //dynamically create an integer, put the memory address in pInt

  *pInt = rand()
  cout << "The value stored at address " << pInt << " is " << *pInt << endl;

  delete pInt;  //dynamically de-allocate/delete/destroy the integer
  pInt = NULL;  //reset the pointer
  cin.get();
  return 0;
}

The keyword new is used to dynamically create an "anonymous" object then store it's address in a pointer. The keyword delete is used to "un-create"/de-allocate/destroy the anonymous object, making the pointer no longer useful for accessing that object. If you don't delete an object created with new, you get what is called a "memory leak". A memory leak is an error than can cause system issues if left unresolved for too long.

Hopefully, that didn't confuse you more.

For more info on pointers, read this.
For more info on dynamic memory, read this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.