How to allocate memory for array..?How to delete that array.? Am i correct in this code?
-
... ... BYTE *pbyCmd; pbyCmd = new BYTE[byCount+1]; ... ... if (pbyCmd) delete pbyCmd; ... ...
Is this correct code. which I am posting here above. Please let me know?
Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother Teresa -
... ... BYTE *pbyCmd; pbyCmd = new BYTE[byCount+1]; ... ... if (pbyCmd) delete pbyCmd; ... ...
Is this correct code. which I am posting here above. Please let me know?
Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother TeresaG Haranadh wrote:
pbyCmd = new BYTE[byCount+1];
Why +1 ?
G Haranadh wrote:
if (pbyCmd) delete pbyCmd;
No. if (pbyCmd) delete [] pbyCmd;
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
... ... BYTE *pbyCmd; pbyCmd = new BYTE[byCount+1]; ... ... if (pbyCmd) delete pbyCmd; ... ...
Is this correct code. which I am posting here above. Please let me know?
Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother TeresaBYTE *pbyCmd
= NULL
; //Always allocate a default value,
// or directly assign a valuepbyCmd = new BYTE[byCount+1]; //why +1 ?
if (pbyCmd) {
delete**[]
** pbyCmd;
pbyCmd = NULL;
//this is not done automatically by delete
// but you should do it for security, and if
// you want to be able to test if NULL or not
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
G Haranadh wrote:
pbyCmd = new BYTE[byCount+1];
Why +1 ?
G Haranadh wrote:
if (pbyCmd) delete pbyCmd;
No. if (pbyCmd) delete [] pbyCmd;
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Christian Graus wrote:
Why +1 ?
This is existing project. And we need one byte extra allocation at that situation. :) Thanks.
Christian Graus wrote:
if (pbyCmd) delete [] pbyCmd;
Thanks:-D. I am replacing this code with this. :cool:
Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother Teresa -
BYTE *pbyCmd
= NULL
; //Always allocate a default value,
// or directly assign a valuepbyCmd = new BYTE[byCount+1]; //why +1 ?
if (pbyCmd) {
delete**[]
** pbyCmd;
pbyCmd = NULL;
//this is not done automatically by delete
// but you should do it for security, and if
// you want to be able to test if NULL or not
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Good point, how did I forget it ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
BYTE *pbyCmd
= NULL
; //Always allocate a default value,
// or directly assign a valuepbyCmd = new BYTE[byCount+1]; //why +1 ?
if (pbyCmd) {
delete**[]
** pbyCmd;
pbyCmd = NULL;
//this is not done automatically by delete
// but you should do it for security, and if
// you want to be able to test if NULL or not
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Thanks.:rose:After long time we met:cool:. Ok If i said
pbyCmd = new BYTE[1];
then?delete pbyCmd; //or delete[] pbyCmd;
Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother Teresa -
Thanks.:rose:After long time we met:cool:. Ok If i said
pbyCmd = new BYTE[1];
then?delete pbyCmd; //or delete[] pbyCmd;
Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother Teresa -
BYTE *pbyCmd
= NULL
; //Always allocate a default value,
// or directly assign a valuepbyCmd = new BYTE[byCount+1]; //why +1 ?
if (pbyCmd) {
delete**[]
** pbyCmd;
pbyCmd = NULL;
//this is not done automatically by delete
// but you should do it for security, and if
// you want to be able to test if NULL or not
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
if (pbyCmd) { delete[] pbyCmd;
Checking for
NULL
while deleting pointers feels unnecessary. There is no harm deletingNULL
pointers. And if its dangled/uninitialized pointer, that will cause problem any way.Prasad Notifier using ATL | Operator new[],delete[][^]