C++ Note

C++ Note

  • new/delete vs malloc/free
    new/delete ( keyword ) call the Constructor/Destructor.
    malloc/free( std. function ) simply allocate memory from the heap.

  • OOP features
    Encapsulation:

    Human 
    {
    public:
         void eat();
    private:
         void openMouth();
         void putFoodIn();
         void closeMouth();
    }
    void Human::eat()
    {
        openMouth();
        putFoodIn();
        closeMouth();
    }

    Abstraction: Essential characteristics of model.

    Inheritance & Polymorphism: Women and Man , since they are part of Human, so both can speak(), but their voice is different.

  • union
    The union is only as big as necessary to hold its largest data member.
    A union can have member functions (including constructors and destructors), but not virtual functions.
    A union cannot have base classes and cannot be used as a base class.
    A union cannot have data members of reference types.

  • stack vs heap
    Stack:

    1. Stored in computer RAM just like the heap.
    2. Variables created on the stack will go out of scope and automatically deallocate.
    3. Much faster to allocate in comparison to variables on the heap.
    4. Implemented with an actual stack data structure.
    5. Stores local data, return addresses, used for parameter passing
    6. Can have a stack overflow when too much of the stack is used. (mostly from infinite (or too much) recursion, very large allocations)
    7. Data created on the stack can be used without pointers.
    8. You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.
    9. Usually has a maximum size already determined when your program starts.

    Heap:

    1. Stored in computer RAM just like the stack.
    2. In C, variables on the heap must be destroyed manually and never fall out of scope. The data is freed with delete, delete[], or free
    3. Slower to allocate in comparison to variables on the stack.
    4. Used on demand to allocate a block of data for use by the program.
    5. Can have fragmentation when there are a lot of allocations and deallocations
    6. In C++ data created on the heap will be pointed to by pointers and allocated with new or malloc
    7. Can have allocation failures if too big of a buffer is requested to be allocated.
    8. You would use the heap if you don’t know exactly how much data you will need at runtime or if you need to allocate a lot of data.
    9. Responsible for memory leaks.
  • reference vs pointer

    1. reference have to be initialized.
    2. reference can not be modified(not value).
    3. local reference should not be return value
  • array vs pointer

char amessage[] = "now is the time";
char *pmessage = "now is the time";
amessage[0] = 'N' //OK 
pmessage[0] = 'N' //Compile OK,But run NG

amessage is an array who live in stack.
pmessage is a pointer who live in stack, but its value is commonly stored in a string table.
sizeof(pmessage) is size of pointer but not string length.

  • before main(), global object while be constructed

  • virtual destructor

    class A{
    public:
        ~A(){
            qDebug()<<"A Dead";
        }
    };

    class B: public A{
    public:
        ~B(){
            qDebug()<<"B Dead";
        }
    };

    A* ab = new B();
    delete ab; // A Dead

    //But
    class A{
    public:
        virtual ~A(){
            qDebug()<<"A Dead";
        }
    };
    // A Dead
    // B Dead
  • Stack Overflow
    Eg: infinite recursion.

  • Heap Overflow
    garbage collection.

  • Inline function
    If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.
    So, it decreases the frequency of function call but it increases the size of function which contains the call of inline function .

To be continue…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值