|
-
Jan 5th, 2002, 09:39 AM
#1
Thread Starter
Member
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: ut);
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;
}
-
Jan 5th, 2002, 11:18 AM
#2
Monday Morning Lunatic
Here's your bug:
Code:
char *pName = new char(dwSize);
That's allocating space for ONE element, giving it a value of dwSize. I expect you meant:
Code:
char *pName = new char[dwSize];
Then, you have to delete it using the delete[] operator, not the normal delete:
Code:
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std; // Need this to use iostream anyway!
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");
if(!fout){
cout << "Unable to open file" << endl;
return -1;
}
GetUserName(pName, &dwSize);
fout << pName;
delete[] pName;
fout.close();
return 0;
}
You actually don't even need to use a pointer, since you know the size at compile time:
Code:
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std; // Need this to use iostream anyway!
int main() {
// to retrive the curren user name and write to file
DWORD dwSize =30;
char pName[dwSize];
ofstream fout("C:\\temp\\user.txt");
if(!fout){
cout << "Unable to open file" << endl;
return -1;
}
GetUserName(pName, &dwSize);
fout << pName;
fout.close();
return 0;
}
Note that no delete is necessary now.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 5th, 2002, 11:35 AM
#3
Thread Starter
Member
Thanks parksie
Thanks parksie,
Still learning!
hmm
P.S. I think I seen in one of the posts (archives), a mention of a tutorial on writing dlls written by yourself. True? If so, would you mine if I had a look at it? I could be wrong
Thanks again
-
Jan 5th, 2002, 11:38 AM
#4
Monday Morning Lunatic
I'll have a look for it.
Quick correction: char pName[dwSize];
Replace with: char pName[30];
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 5th, 2002, 11:41 AM
#5
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 5th, 2002, 12:45 PM
#6
Thread Starter
Member
Thanks
hmm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|