delete method and error msg
Subject: delete method and associated error msg
OS: w2000 pro
Compiler VC++ 6
App: Console
Error msg: DAMAGE: after Normal block[430] at 0x00300030
Hi
If I include ‘delete pName’ I receive the above error msg, but if I remove it, no error. I understood you used ‘delete’ release allocated memory i.e. prevent leakage. Am I wrong?
Thanks
hmm
Code:
#include <iostream>
#include <windows.h>
#include <fstream.h>
int main(){
// to retrive the curren user name and write to file
DWORD dwSize =30;
char *pName =new char(dwSize);
ofstream fout("C:\\temp\\user.txt",ios::out);
if(!fout){
cout << "Unable to open file" << endl;
return 0;
}
GetUserName(pName,&dwSize);
fout.write(pName,dwSize);
// the line below causes the error
delete pName;
fout.close();
return 0;
}