Results 1 to 6 of 6

Thread: delete method and error msg

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40

    Question 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;
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  3. #3

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40

    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

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  6. #6

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40

    Talking 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
  •  



Click Here to Expand Forum to Full Width