|
zahid_ash wrote: sock = NULL;
delete sock;
This won't do anything !
You first have to delete the pointer THEN set it to NULL:
delete sock;
sock = NULL;
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
CSocket* pSocket;
// Allocate a memory for pSocket
// Delete a allocated memory
if(pSocket)
{
delete pSocket;
pSocket = NULL;
}
Regards
Amar.
|
|
|
|
|
I think you can even skip the checking for NULL :
delete pSocket;
pSocket = NULL;
This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL .
-- modified at 9:42 Friday 2nd June, 2006
|
|
|
|
|
Viorel Bejan wrote: This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL.
This only happens in debug builds. You should ALWAYS check for null before deleting a pointer. Calling delete NULL has undefined behavior and should be avoided.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
|
Yes, the standard says that. However, most compilers did not meet that standard until recently (and some still don't). It is one of those better safe than sorry things.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: However, most compilers did not meet that standard until recently (and some still don't). It is one of those better safe than sorry things.
Compilers don't meet the Standard mostly in some areas of template handling. Deleting a zero is perfectly safe and has been for quite a while.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Nemanja Trifunovic wrote: Compilers don't meet the Standard mostly in some areas of template handling. Deleting a zero is perfectly safe and has been for quite a while.
Let me put it this way ...
There is a reason why the DirectX libraries define the following macro:
#define SAFE_DELETE(p) if(p) { delete p; p = NULL; }
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: There is a reason why the DirectX libraries define the following macro
Yep, there is: Microsoft DirectX programmers don't know C++ very well.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Zac Howland wrote: Calling delete NULL has undefined behavior and should be avoided.
Nope, delete NULL is required by the C++ Standard to do nothing.
It is perfectly safe.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Always NULL the pointer after deleting it. Otherwise you could (if you're not very, very careful) try to delete the same pointer twice, which will result in an access violation.
Deleting a NULL pointer however is perfectly safe.
|
|
|
|
|
Another reason for doing that is that the address used for the recently-deallocated memory may still be valid in your address space and while not technically valid for use, accessing it might not cause an IPF or Access Violation. For example:
TCHAR *pcBuffer = new TCHAR[ 1024 ];
delete [] pcBuffer;
pcBuffer[ 1 ] = _T( 'A' ); The above code may not crash even though the pointer is technically invalid.
By setting it to NULL , you just about guarantee that accessing it will cause an Access Violation (at least if on Win32 and if the access range of the pointer is < 4096 , because that hits the reserved "NULL pointer page" which causes an instant exception, IIRC).
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Can anyone tell me the function call to get the path of the application... i cant for the life of me remember..
Thanks
Lee
|
|
|
|
|
See GetModuleFileName
whitesky
|
|
|
|
|
|
|
The current directory and the "path of the application" are not necessarily the same thing. Most requests are for the latter.
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
Hi All,
I want to make package for my project, can anyone help me with this..so that i can install in other system.
Regards,
Vinay Charan.
|
|
|
|
|
|
You can try one of the following setup packagers (free):
NSIS[^]
WiX[^]
|
|
|
|
|
I would be very grateful to anyone who could give me some easy code to tell me how timers can be used in a MFC application in VC++6.0...
MSDN is not that helpful in the matter as i am getting only .NET hits...
I need to be able to call a purticular member function of the dialog class on timer event..
Sharath
|
|
|
|
|
|
Define a timer id
#define ID_MYTIMER 201
Use SetTimer to set and initiate the timer .
SetTimer(ID_MYTIMER,1000,NULL);//1000 is the mseconds
Put your code in the OnTimer function . You can find it using the Class Wizard.
Use KillTimer() to stop the timer
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
-- modified at 7:22 Friday 2nd June, 2006
|
|
|
|
|
There is one important thing to remember when dealing with Windows timers that people often forget. The SetTimer function returns the ID of the created timer. There is no guarantee that the actual returned ID of the created timer will be the same ID as requested.
The return value of SetTimer(...) should be used to identify the timer in the timer-handling function and when destroying the timer.
(Note that the link provided below to http://www.codersource.net/mfc_working_with_timers.html does this correctly, although it does something really stupid in the OnTimer(...) by using dynamically allocated memory for no good reason...)
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|