Click here to Skip to main content
15,892,965 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Address Bar Pin
Liger_Zero_X15-Jul-04 21:34
Liger_Zero_X15-Jul-04 21:34 
GeneralRe: Address Bar Pin
David Crow16-Jul-04 3:01
David Crow16-Jul-04 3:01 
GeneralPointer Usage Pin
IceBerG7115-Jul-04 16:54
IceBerG7115-Jul-04 16:54 
GeneralRe: Pointer Usage Pin
Michael Dunn15-Jul-04 17:21
sitebuilderMichael Dunn15-Jul-04 17:21 
GeneralRe: Pointer Usage Pin
Anonymous15-Jul-04 18:38
Anonymous15-Jul-04 18:38 
GeneralRe: Pointer Usage Pin
Antti Keskinen15-Jul-04 19:57
Antti Keskinen15-Jul-04 19:57 
GeneralRe: Pointer Usage Pin
David Crow16-Jul-04 3:09
David Crow16-Jul-04 3:09 
GeneralRe: Pointer Usage Pin
digwizfox16-Jul-04 10:37
digwizfox16-Jul-04 10:37 
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 only uses the string within the block of the function, you might as well make it static or just make the string local to that scope in your program. If the string needs to continue to exist after the function completes, you have to use 'new' otherwise the string will not exist after the function call. lets say you are passing the string to another function and then the function creating the string ends. Well if you don't use new in that case, the string goes out of scope when the function creating it ends and the function you passed the pointer to is out of luck and has a dangling reference to an object that's been deallocated.

I know nothing about your application and have no idea whether to use new or delete vs. a static character array. It all depends on what you need the string for. It's totally situational. Do not be afraid to use new and delete if you have a legitimate purpose for doing so. That is why the operations exist.
GeneralWM_NCCALCSIZE and WM_NCPAINT madness Pin
Jim Crafton15-Jul-04 15:18
Jim Crafton15-Jul-04 15:18 
GeneralRe: WM_NCCALCSIZE and WM_NCPAINT madness Pin
Johan Rosengren15-Jul-04 22:53
Johan Rosengren15-Jul-04 22:53 
GeneralTrapping mouse down and mouse up with CMenu's TrackPopupMenu() Pin
jeffb4215-Jul-04 14:13
jeffb4215-Jul-04 14:13 
QuestionHow to create a 16 Color (4-bit) Bitmap? Pin
Pinhead_Me15-Jul-04 13:19
Pinhead_Me15-Jul-04 13:19 
GeneralCListCtrl DeleteColumn Pin
Anthony988715-Jul-04 12:44
Anthony988715-Jul-04 12:44 
GeneralRe: CListCtrl DeleteColumn Pin
Jörgen Sigvardsson15-Jul-04 13:03
Jörgen Sigvardsson15-Jul-04 13:03 
GeneralRe: CListCtrl DeleteColumn Pin
Anthony988715-Jul-04 13:06
Anthony988715-Jul-04 13:06 
GeneralRe: CListCtrl DeleteColumn Pin
Jim Crafton15-Jul-04 16:07
Jim Crafton15-Jul-04 16:07 
GeneralApplication stuck when a window is moved Pin
Aragorn@Gondor15-Jul-04 12:23
Aragorn@Gondor15-Jul-04 12:23 
GeneralRe: Application stuck when a window is moved Pin
Anders Molin15-Jul-04 12:29
professionalAnders Molin15-Jul-04 12:29 
GeneralRe: Application stuck when a window is moved Pin
Jörgen Sigvardsson15-Jul-04 13:07
Jörgen Sigvardsson15-Jul-04 13:07 
Generalcompiling errors with xutility header file Pin
zlatnik15-Jul-04 12:02
zlatnik15-Jul-04 12:02 
GeneralRe: compiling errors with xutility header file Pin
vmaltsev15-Jul-04 12:30
vmaltsev15-Jul-04 12:30 
GeneralRe: compiling errors with xutility header file Pin
zlatnik15-Jul-04 12:37
zlatnik15-Jul-04 12:37 
GeneralRe: compiling errors with xutility header file Pin
vmaltsev15-Jul-04 12:41
vmaltsev15-Jul-04 12:41 
GeneralRe: compiling errors with xutility header file Pin
zlatnik15-Jul-04 12:47
zlatnik15-Jul-04 12:47 
GeneralRe: compiling errors with xutility header file Pin
vmaltsev15-Jul-04 12:57
vmaltsev15-Jul-04 12:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.