BlGene
BlGene

Reputation: 17

How does RAII work when mixing default and non-default constructors

I want to write a library wrapper class (LibWrap) around a C library that uses malloc/free. To do this I want to use C++'s RAII to allocate and free memory. I used lib_address as a random example address that I would receive from the library. However when defining my memeber variable the destructor that is called somehow has this lib_address.

I would expect the destructor of the member variable created by the default constructor not to know the new address that I am putting into the constructor of my replacement member variable.

#include <stdlib.h>
#include <iostream>
using namespace std;

class LibWrap
{
    int j;
    int lib_address;
public:
    LibWrap(): //default LibWrap
    j(0),
    lib_address(0)
    {
        cout << "default LibWrap "<<j <<"\t\t"<<lib_address << "\t" << this<<endl;
    }

    LibWrap(int f_j): //special LibWrap
    j(0),
    lib_address(0)
    {
        j = f_j;
        lib_address = rand();
        cout << "special LibWrap " << j<<"\t"<< lib_address<< "\t" << this <<endl;
    }

    ~LibWrap()
    {
        cout << "killing LibWrap " << j<<"\t" <<lib_address <<"\t" << this<< endl;
    }

    int g()
    {
        return j;
    }
};

class A
{
    int i;
    LibWrap b;

public:
    A(): //default A
    i(0)
    {
        cout << "default A\t"<<i << endl;
    }

    A(int f_i)://special A
    i(0)
    {
        i = f_i;
        cout << "special A\t"<<i << endl;
        b = LibWrap(10);
    }
    ~A()
    {
        cout << "killing A\t"<<i << endl;
    }


    void p()
    {
        cout <<"Test values: "<< i<< "," << b.g() << endl;
    }
};

int f()
{
    //A a; a.p();
    cout << "variable\t\tlib_address\treal_address" << endl; 
    A a = A(1);
    cout << "End" << endl;
    //a.p();
}

int main()
{
    f();
}

Running this code I would expect to get the following result:

variable        lib_address real_address
default LibWrap 0       0   0xbfef2e28
special A   1
special LibWrap 10  1804289383  0xbfef2df8 
killing LibWrap 10  1804289383  0xbfef2df8 --would expect kiling LibWrap 0  0 0xbfef2e28
End
killing A   1
killing LibWrap 10  1804289383  0xbfef2e28 --would expect killing LibWrap 10 1804289383 0xbfef2df8

Upvotes: 0

Views: 147

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473537

b = LibWrap(10);

This does not initialize b. b has already been initialized as part of constructing A. What you're doing is creating a temporary LibWrap and copying that temporary into b. Then, you're destroying that temporary LibWrap (which is where the extra destructor call with the lib_address comes from).

The temporary LibWrap is where the "0xbfef2df8" address comes from. The b variable is the "0xbfef2e28" address. That's why you're getting them in that order.

Upvotes: 4

Related Questions