Pointer Usage
-
Hi Just a question on Pointer In C++ used on embedded system, memory leak is very critical. so i used example LPTSTR ptrText = new _TCHAR[MAX_PATH]; when i exit, i use if(ptrText) { delete[] ptrText; ptrText = 0; } Just wonder if the ptrText = 0; is necessary? Another is will the use of new and delete slow down the system?
-
Hi Just a question on Pointer In C++ used on embedded system, memory leak is very critical. so i used example LPTSTR ptrText = new _TCHAR[MAX_PATH]; when i exit, i use if(ptrText) { delete[] ptrText; ptrText = 0; } Just wonder if the ptrText = 0; is necessary? Another is will the use of new and delete slow down the system?
IceBerG71 wrote: Just wonder if the ptrText = 0; is necessary? It is not necessary, however it is a good habit to do so, because if some code later tries to dereference
ptrText
, after thedelete[]
, the app will crash immediately and you will catch the bug. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- "Linux is good. It can do no wrong. It is open source so must be right. It has penguins. I want to eat your brain." -- Paul Watson, Linux Zombie -
IceBerG71 wrote: Just wonder if the ptrText = 0; is necessary? It is not necessary, however it is a good habit to do so, because if some code later tries to dereference
ptrText
, after thedelete[]
, the app will crash immediately and you will catch the bug. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- "Linux is good. It can do no wrong. It is open source so must be right. It has penguins. I want to eat your brain." -- Paul Watson, Linux Zombie -
Thank You. So Is There Performance Related Issue When using new & delete rather than just use the normal char declaration?
There is not. When you declare normal variables, the memory they take is reserved from the stack. On mobile systems, the stack memory is extremely limited, so heap memory should be used instead. New & delete operators operate on the heap memory area. There should be no performance hit, although reserving large quantities of memory (over 1 Mb) may cause small performance hits. So, if you need to reserve large quantities of memory, you should do that in sections whenever possible. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
-
Thank You. So Is There Performance Related Issue When using new & delete rather than just use the normal char declaration?
Anonymous wrote: So Is There Performance Related Issue When using new & delete rather than just use the normal char declaration? Yes. One requires help from the memory manager, while the other does not. However, the "penalty" is negligible. It's also not uncommon for the
new
operator to be used incorrectly. For example:char *name = new char[32];
lstrcpy(name, _T("David"));
m_listbox.AddString(name);
delete [] name;This is highly unnecessarily. Consider this instead:
char name[32] = _T("David"); // or char *name = _T("David");
m_listbox.AddString(name);
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
Thank You. So Is There Performance Related Issue When using new & delete rather than just use the normal char declaration?
I would argue that setting the pointer variable back to NULL after deleting the object is critical and highly recommended. If you do not, you are leaving a dangling reference to something that no longer exists. Once the memory is deallocated, by delete, the memory can be used for something else by another part of the program. If someone else is checking the pointer value against NULL, then the reset of the ptr to NULL will prevent a crash. My philosophy is that if the pointer doesn't point to anything then it should be null. NEVER EVER leave a pointer set to an address that is no longer pointing to a valid object. That is just asking for trouble in my mind. Of course deleting an object in the middle of program execution is tricky to begin with. There has to be an understanding between the piece of software responsible for deleting the object and the piece(s) of software using the pointer. Setting the pointer back to NULL is a simple way of establishing that understanding. If the pointer is NULL, don't use it! //perhaps this is done in the constructor of a class and the pointer //mlpszFilePath is a member variable of some class. LPTSTR mlpszFilePath = NULL; //Now in some member function you do this: //Only build the string if it has not already been built if(lpszFilePath == NULL) { mlpszFilePath = new _TCHAR chFilePath[_MAX_PATH]; } //later you'd do this: //Only delete the array if it has been previously allocated; perhaps in a class destructor. if (mlpszFilePath != NULL) { delete[] mlpszFilePath ; mlpszFilePath = NULL; } //Perhaps you have a function to get the pointer const LPCTSTR GetPointer() const { return mlpszFilePath; } Perhaps a string is a bad example but you might have this kind of code where you have to new and delete some object that is placed in the global scope via a pointer variable that many components of an application need access to. Notice that the setup allows the same object to be reused over and over. This type of pattern is very common and sort of applies to your other question about performance surrounding new and delete. Yes, new and delete require system resources like any function call or calculation; but the operating system is designed to perform memory management for you so that you don't have to worry about it. If you are only rarely using new and delete to allocate a string it's not a big deal. If you are doing it thousands of times in a second, then that could be a problem. If the function using the string o