Thread: Shouldn't I free the memory I malloced in constructors?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    55

    Shouldn't I free the memory I malloced in constructors?

    I have this inside my constructor:

    Code:
    	ippFile = NULL;
    	fileName = (char*) malloc(strlen(name));
    	strcpy(fileName,name);

    And this in my destructor:

    Code:
    free(fileName);


    When that destructor is called I get the following message:

    Code:
    Microsoft Visual C++ Debug Library
    --------------------------------------------
    Debug Error!
    
    Program: xxxxxxx.exe
    
    DAMAGE: after Normal block (#41) at 0x003F0860
    
    
    (Press Retry to debug the aplication)

    Now. If I don't do that free, everything runs smoothly... What is going on?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fileName = (char*) malloc(strlen(name));
    1. You didn't count the \0, so the strcpy trashed some memory
    2. Use new/delete for allocating memory in C++

    strlen(name)+1 is how many you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    fileName = new char[strlen(name) + 1];



    Like that, right?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes

    And
    delete [ ] fileName;
    in the destructor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There's no excuse for not using std::string for this.

  6. #6
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    there is of course. i'm not gonna explain that. there could be plenty of reasons for someone to prefer a normal string.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pronecracker View Post
    there is of course. i'm not gonna explain that. there could be plenty of reasons for someone to prefer a normal string.
    Sure.. But maybe at least use new instead of malloc? Unless you know you're going to be using realloc() later, I'd say use new.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> there could be plenty of reasons for someone to prefer a normal string.

    Unfortunately the most common reasons are based on the antiquated notion that a null terminated character array is a normal string in C++. The C++ string class is the closest thing to a normal string in C++, and in many cases (especially on forums such as these) the excuses for using C style are poor.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    My excuse is that I'm a C programmer who has started using C++ a week ago...

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's a better excuse than most. Although if you really want to learn C++ I'd suggest learning the C++ string class (and vector and other standard C++ library tools).

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by brewbuck View Post
    Sure.. But maybe at least use new instead of malloc? Unless you know you're going to be using realloc() later, I'd say use new.
    I would like to mention that even then it isn't necessary to use C functions. It's even less necessary to do this at all I suppose, but
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    void print( int base[], std::size_t size )
    {
        for( std::size_t i = 0; i < size; ++i )
        {
            std::cout << ' ' << base[ i ];
        }
        std::cout << std::endl;
    }
    
    int main( )
    {
        const std::size_t ORIG_SIZE = 4;
        const std::size_t NEW_SIZE = 8;
    
        // 1) Build an array of some size.
        int *bar = new int[ ORIG_SIZE ];
        std::time_t now;
    
        std::time( &now );
        std::srand( now );
    
        // 2) Use the array however you like.
        for( std::size_t i = 0; i < ORIG_SIZE; ++i )
            bar[ i ] = std::rand( );
    
        std::cout << "Before: ";
        print( bar, ORIG_SIZE );
    
        // 3) Make a temporary array.
        int *temp_bar = new int[ ORIG_SIZE ];
        // 4) Copy objects over to this array.
        for( std::size_t i = 0; i < ORIG_SIZE; ++i )
            temp_bar[ i ] = bar[ i ];
    
        // 5) Extend the array with new after deleting the old array data.
        delete [] bar;
        bar = new int[ NEW_SIZE ];
        // 6) Refill the extended array.
        for( size_t i = 0; i < NEW_SIZE; ++i )
            bar[ i ] = ( i < ORIG_SIZE )? temp_bar[ i ] : std::rand( );
    
        delete [] temp_bar;
    
        std::cout << "After : ";
        print( bar, NEW_SIZE );
        // and so on ...
    
        // 7) Clean up when you're done!
        delete [] bar;
    }
    
    /*
    * My output:
    *
    * Before:  24197 24557 21828 14805
    * After :  24197 24557 21828 14805 20141 15933 21001 9472
    */
    Sorry about the bump for this...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  4. Memory leak with detached pthreads - how to free?
    By rfk in forum Linux Programming
    Replies: 2
    Last Post: 08-17-2007, 06:50 AM
  5. Replies: 12
    Last Post: 06-24-2005, 04:27 PM