| | |
Deleting a pointer?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I have a rather simple question, and I' ve been looking for the answer for awhile, but did not find any.
So I would like to know, what is the proper way of deleting a pointer.
For example I have a class with some kind of array, which I want to extend later. But what if, for some reason, I won't have to. Like, I am opening a file, but it is not there, so I don't need to allocate any memory. Eveything is OK, then destructor starts working and then the whole program crashes. Altough It works fine if it had some amount of memory allocated before.
So would you please show me the proper way of deleting pointers?
Thank you very much.
I didn't want to off, so if it is just kill it. 'SOORY'
So I would like to know, what is the proper way of deleting a pointer.
For example I have a class with some kind of array, which I want to extend later. But what if, for some reason, I won't have to. Like, I am opening a file, but it is not there, so I don't need to allocate any memory. Eveything is OK, then destructor starts working and then the whole program crashes. Altough It works fine if it had some amount of memory allocated before.
So would you please show me the proper way of deleting pointers?
Thank you very much.
C++ Syntax (Toggle Plain Text)
myClass::~myClass() { if( my_array != 0 ) { delete []my_array; } }
"Just because I'm losing, doesn't mean I'm lost. Doesn't mean I'll stop."
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
The question should be asked "how do I delete a pointer if I allocate the memory using xxxx?"
Let us review:
if you write
but if you write
Note as pointed out, delete removes the memory for the pointer BUT does not change the value of the pointer variable. e.g.
Note in the above example , if you set
Finally::
If the pointer is from an object that is not allocated via a new then don't delete it.
[Note: that if you are using malloc etc, then you must use free. If you are not using malloc, then don't start using it. ]
Hope this helps
Let us review:
if you write
c++ Syntax (Toggle Plain Text)
double* Ptr=new double[50]; // Allocate space for 50 double s // ... other code delete [] Ptr; // Delete the pointer to 50 doubles // CARE: // Ptr is still pointing to a value
but if you write
c++ Syntax (Toggle Plain Text)
double* Ptr=new double; // allocate 1 double //.... other code delete Ptr;
Note as pointed out, delete removes the memory for the pointer BUT does not change the value of the pointer variable. e.g.
C++ Syntax (Toggle Plain Text)
; std::cout<<"Ptr memory location == "<<reinterpret_cast<long int>(Ptr)<<std::endl; delete [] Ptr; std::cout<<"Ptr memory location == "<<reinterpret_cast<long int>(Ptr)<<std::endl; // // THIS CAUSES A RUNTIME ERROR: // DO NOT DELETE TWICE // delete [] Ptr;
Note in the above example , if you set
Ptr=0; after the first delete then all is well.Finally::
c++ Syntax (Toggle Plain Text)
double X[50]; //.... code // NO DELETE Required: // THIS CAUSES A RUNTIME ERROR: delete [] X;
If the pointer is from an object that is not allocated via a new then don't delete it.
[Note: that if you are using malloc etc, then you must use free. If you are not using malloc, then don't start using it. ]
Hope this helps
experience is the most expensive way to learn anything
![]() |
Similar Threads
- Deleting this pointer (C++)
- Deleting a pointer? (C++)
- How to be Crash Free (C++)
- storing references to classes (C++)
Other Threads in the C++ Forum
- Previous Thread: How do we start making games?
- Next Thread: C++ app made in Dev-C++ uses OpenCV and freezes up...
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






