Tuesday, 29 July 2014

[C++] Why we need new/delete in C++? Can't malloc/free do the same thing?

The difference between malloc/free and new/delete is malloc/free are standard library, and new/delete are operator.
#include <iostream>
#include <cstdlib>

using namespace std;

class Obj
{
public :
    Obj(void){ cout << "Constructor" << endl; }
    ~Obj(void){ cout << "Destructor" << endl; }
    void Initialize(void){ cout << "Initialization" << endl; }
    void Destroy(void){ cout << "Destroy" << endl; }
};

void UseMallocFree(void)
{
    Obj  *a = (Obj *)malloc(sizeof(Obj));
    a->Initialize();

    //…

    a->Destroy();
    free(a);

}

void UseNewDelete(void)
{
    Obj  *a = new Obj;

    //…

    delete a;
}

int main()
{
    cout << "UseMallocFree" << endl;
    UseMallocFree();
    cout << endl << "UseNewDelete" << endl;
    UseNewDelete();

    return 0;
}
UseMallocFree
Initialization
Destroy

UseNewDelete
Constructor
Destructor

Ref.: http://fanqiang.chinaunix.net/a4/b2/20020722/060200273_b.html

Monday, 28 July 2014

[C++] Constructor/Copy Constructor/Assignment Operator/Destructor

1) Both Copy Constructor and Assignment Operator are call by address, not call by value
#include <iostream>

using namespace std;

class Array {
    public:
        int *_array;
        int size;

        Array(int input):size(input)
        {
            _array = new int[size];
        }

        void Set(int i, int v)
        {
            _array[i] = v;
        }

        int Get(int i)
        {
            return _array[i];
        }

        ~Array()
        {
            delete [] _array;
        }
};

int main()
{
    Array a(5);
    Array b = a;

    a.Set(0, 10);
    a.Set(1, 20);
    a.Set(2, 30);
    a.Set(3, 40);
    a.Set(4, 50);

    cout << a.Get(0) << " " << a.Get(1) << " " << a.Get(2) << " " << a.Get(3) << " " << a.Get(4) << endl;
    cout << b.Get(0) << " " << b.Get(1) << " " << b.Get(2) << " " << b.Get(3) << " " << b.Get(4) << endl;
    cout << &a << " " << a._array << endl;
    cout << &b << " " << b._array << endl;

    return 0;
}
10 20 30 40 50
10 20 30 40 50
0x7fff61f44a90 0x1180010
0x7fff61f44aa0 0x1180010
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0000000001180010 ***

the problem caused in this program:
- memory leak in b
- it was copied by "b._array = a._array;", either modified in b or a will change the result
- double free

2) Initialization list
#include <iostream>

using namespace std;

class A {
    public:
        A()
        {
            cout << "A constructor" << endl;
        }

        A(const A& a)
        {
            cout << "A copy constructor" << endl;
        }

        A& operator=(const A& a)
        {
            cout << "A assignment Operator" << endl;
        }
};

class B {
    public:
        B(A &input)
        {
            cout << "B consturctor" << endl;
            m_a = input;
        }

    private:
        A m_a;
};

class C {
    public:
        C(A& input):m_a(input)
        {
            cout << "C constructor" << endl;
        }

    private:
        A m_a;
};

int main()
{
    cout << "===A===" << endl;
    A a;
    cout << "===B===" << endl;
    B b(a);
    cout << "===C===" << endl;
    C c(a);

    return 0;
}
===A===
A constructor
===B===
A constructor
B consturctor
A assignment Operator
===C===
A copy constructor
C constructor

3) Const variable can only be initialed in initialization list
#include <iostream>

using namespace std;

class A {
    public:
        A(int input) { a = input; }
        void show() { cout << a << endl; }

    private:
        const int a;
};

int main()
{
    A aa(5);
    aa.show();

    return 0;
}
test2.cpp: In constructor ‘A::A(int)’:
test2.cpp:7:9: error: uninitialized member ‘A::a’ with ‘const’ type ‘const int’ [-fpermissive]
test2.cpp:7:28: error: assignment of read-only member ‘A::a’
#include <iostream>

using namespace std;

class A {
    public:
        A(int input):a(input) { }
        void show() { cout << a << endl; }

    private:
        const int a;
};

int main()
{
    A aa(5);
    aa.show();

    return 0;
}
5

4) The serial of constructor and destructor
#include <iostream>

using namespace std;

class A {
    public:
        A()
        {
            cout << "A constructor" << endl;
        }

        ~A()
        {
            cout << "A destructor" << endl;
        }
};

class B {
    public:
        B()
        {
            cout << "B constructor" << endl;
        }

        ~B()
        {
            cout << "B destructor" << endl;
        }
};

class C {
    public:
        C()
        {
            cout << "C constructor" << endl;
        }

        ~C()
        {
            cout << "C destructor" << endl;
        }
};

class Aa : public A {
    public:
        Aa(int in1):y(in1)
        {
            cout << "Aa constructor" << endl;
        }

        ~Aa()
        {
            cout << "Aa destructor" << endl;
        }

    private:
        B b;
        int y;
        C c;
};

int main()
{
    Aa hi(5);

    return 0;
}
A constructor
B constructor
C constructor
Aa constructor
Aa destructor
C destructor
B destructor
A destructor

Saturday, 26 July 2014

[C++] The difference between class and struct

class X {
    int a; // X::a is private by default
};

struct S {
    int a; // S::a is public by default
};

[note]: the variable in union is also public by default.



Ref. Working Draft, Standard for Programming Language C++

Monday, 14 July 2014

Jeff Bezos delivers graduation speech at Princeton University




How will you use your gifts? What choices will you make?

Will inertia be your guide, or will you follow your passions?

Will you follow dogma, or will you be original?

Will you choose a life of ease, or a life of service and adventure?

Will you wilt under criticism, or will you follow your convictions?

Will you bluff it out when you're wrong, or you apologize?

Will you guard your heart against rejection, or will you act when you fall in love?

Will you play it safe, or will you be a little bit swashbuckling?

When it's tough, will you give up, or will you be relentless?

Will you be a cynic, or will you be a builder?

Will you be clever at the expense of others, or will you be kind?