C++ Primer 读书笔记 - 第五章

这一章的内容和C语言基础知识大同小异。

1. i++ 与 ++i 的效率问题

    i++的话,需要保存原来的值,然后增加 i,之后返回 i 原来的值;++i 直接增加 i,然后返回当前的 i 值,所以少做一步工作。

2. Setting the pointer to 0 after the object it refers to has been deleted makes it clear that the pointer points to no object.

    It is legal to delete a pointer whose value is zero; doing so has no effect.

#include 
#include 
using namespace std;

void print(int a[])
{
    cout << sizeof(a)/sizeof(*a) << endl;
}

int main()
{
    int a[9] = {0};
    cout << sizeof(a)/sizeof(*a) << endl;
    print(a);
    
    int *arr = new int[9];
    free(arr);
    arr = NULL;
    delete [] arr;
    
    int *b = (int *)malloc(sizeof(int));
    delete b;
    b = NULL;
    free(b);
    return 0;
}

 

你可能感兴趣的:(C++ Primer 读书笔记 - 第五章)