delete operator
-
Hi, I wrote this code in the Timer which is giving an error while it is running.
char \*wFile = new char\[22\]; wFile = "010B000304140601000000"; WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL); delete\[\] wFile;
I know that delete operator causes this error.How can I solve this? Thanks
-
Hi, I wrote this code in the Timer which is giving an error while it is running.
char \*wFile = new char\[22\]; wFile = "010B000304140601000000"; WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL); delete\[\] wFile;
I know that delete operator causes this error.How can I solve this? Thanks
iayd wrote:
is giving an error
WHICH error ? how can we guess if you don't tell us all we need to know ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hi, I wrote this code in the Timer which is giving an error while it is running.
char \*wFile = new char\[22\]; wFile = "010B000304140601000000"; WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL); delete\[\] wFile;
I know that delete operator causes this error.How can I solve this? Thanks
Woaw, that's very bad code X| First, you allocate a buffer of 22 characters and tries to assign 22 characters in it (you didn't take into consideration the terminating zero). BUT, you in fact don't copy the string into your buffer. You simply just assign a pointer into wFile (your string is in fact a pointer). Later you try to delete this pointer, which of course will fail because your string was not allocated on the heap (not allocated with new). So, two things to take into consideration: 1) Always make your buffer 1 place larger to be able to store the null-terminating zero 2) Use strcpy whenever you want to copy a string. This will copy the contents of your string and not simply make a pointer assignement.
Cédric Moonen Software developer
Charting control [v1.4] -
Hi, I wrote this code in the Timer which is giving an error while it is running.
char \*wFile = new char\[22\]; wFile = "010B000304140601000000"; WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL); delete\[\] wFile;
I know that delete operator causes this error.How can I solve this? Thanks
strlen is expecting a null terminated string, your new statement allocates 22 entries, your assignment fills all 22 places with non null data, I think you have to make the first line something like char[23] enough space to include the terminating null character.
-
Hi, I wrote this code in the Timer which is giving an error while it is running.
char \*wFile = new char\[22\]; wFile = "010B000304140601000000"; WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL); delete\[\] wFile;
I know that delete operator causes this error.How can I solve this? Thanks
iayd wrote:
wFile = "010B000304140601000000";
The above line is a mistake. You should do
char *wFile = new char[23];
strcpy(wFile, "010B000304140601000000");BTW even correct, the above code is wasteful, using a two lines you can do all the stuff:
const char * pStr = "010B000304140601000000";
WriteFile(HD,(LPVOID) pStr , strlen(pStr),&Bytes,NULL);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Friday, May 16, 2008 10:49 AM
-
Hi, I wrote this code in the Timer which is giving an error while it is running.
char \*wFile = new char\[22\]; wFile = "010B000304140601000000"; WriteFile(HD,wFile,strlen(wFile),&Bytes,NULL); delete\[\] wFile;
I know that delete operator causes this error.How can I solve this? Thanks
iayd wrote:
char *wFile = new char[22];
wFile
points to an address that can hold 22 characters.iayd wrote:
wFile = "010B000304140601000000";
You've now assigned
wFile
to a different address.iayd wrote:
delete[] wFile;
delete
is expecting to delete from the address returned fromnew
, but will fail becausewFile
points someplace else."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
iayd wrote:
char *wFile = new char[22];
wFile
points to an address that can hold 22 characters.iayd wrote:
wFile = "010B000304140601000000";
You've now assigned
wFile
to a different address.iayd wrote:
delete[] wFile;
delete
is expecting to delete from the address returned fromnew
, but will fail becausewFile
points someplace else."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne