Hey I am currently teaching myself C++ programming and I have a question about when I need to delete pointers to free up memory. I have been going through the examples in my book I believe I somewhat understand what pointers are used for and also have read that eventually I will know better when I should use them though I am wondering do you always have to delete a pointer before the program ends? The examples I have been doing always delete the pointer at the very end of the program and I was wondering is this actually something needed to be done so the pointer does not eat up memory even after the program is done running or is it just something that is done in the examples to help get people into the habit of deleting their pointers?
This is one of the examples that I am talk about how it has the "delete ps;" right at the end.
If anyone is able to answer or even just give me advice on pointers in general it would be fairly helpful because out of all of the topics I have learned about so far I understand pointers the least.
// newstrct.cpp -- using new with a structure
struct inflatable //definition of structure
using namespace std;
inflatable *ps = new inflatable; //allot memory for structure
cout<< " Etner name of inflatable item: ";
cin.get(ps->name, 20); //method 1 for member access
cout<< "Enter volume in cubic feet: ";
cin >> (*ps).volume; //method 2 for member access
cout<< "enter price: $";
cin >> ps -> price;
cout<< "Name: " << (*ps).name << endl; //method 2
cout<< "Volume: " << ps->volume << " cubic feet\n"; //method 1
cout<< "Price: $" << ps ->price << endl; //method 1
delete ps; //free memory used by structure