• 0-NULL-nullptr


    NULL

    In C

    A null-pointer constant is an integral constant expression that evaluates to zero (like 0 or 0L), or the cast of such value to type void* (like (void*)0).
    

    In C++98

    A null-pointer constant is an integral constant expression that evaluates to zero (such as 0 or 0L).
    

    In C++11

    A null-pointer constant is either an integral constant expression that evaluates to zero (such as 0 or 0L), or a value of type nullptr_t (such as nullptr).
    

    From the description of cpluscplus reference, we can see that NULL's definition is compiler dependent. In this blog, I only discuss the behavior of gcc in linux operating system.

    // file test.c
    #include<stdio.h>
    
    void func(int* p) {
        if (p) *p = 1;
    }
    
    int main()
    {
        int* p = 0;//NULL;
        func(NULL);
    
        return 0;
    }
    

    use gcc to see what's NULL is:

    gcc -E test.c | less
    

    result:

    # 2 "test.c" 2
    
    void func(int* p) {
        if (p) *p = 1;
    }
    
    int main()
    {
        int* p = 0;
        func(((void *)0));
    
        return 0;
    }
    

    we can see that in C, NULL is (void*)0;

    In C++

    #include<iostream>
    
    void func(int* p) {
        if (p) *p = 1;
    }
    
    int main()
    {
        int* p = 0;//NULL;
        func(NULL);
        std::cout << p << std::endl;
    
        return 0;
    }
    

    result:

    void func(int* p) {
        if (p) *p = 1;
    }
    
    int main()
    {
        int* p = 0;
        func(__null);
        std::cout << p << std::endl;
    
        return 0;
    }
    

    it's __null, a integral constant expression.

    The only change that might affect people is the type of NULL: while it is required to be a macro, the definition of that macro is not allowed to be (void*)0, which is often used in C.
    
    For g++, NULL is #define'd to be __null, a magic keyword extension of g++.
    
    The biggest problem of #defining NULL to be something like “0L” is that the compiler will view that as a long integer before it views it as a pointer, so overloading won't do what you expect. (This is why g++ has a magic extension, so that NULL is always a pointer.)
    

    nullptr

    typedef decltype(nullptr) nullptr_t;
    Null pointer type (C++)
    Type of the null pointer constant nullptr.
    
    This type can only take one value: nullptr, which when converted to a pointer type takes the proper null pointer value.
    
    Even though nullptr_t it is not a keyword, it identifies a distinct fundamental type: the type of nullptr. As such, it participates in overload resolution as a different type.
    
    This type is only defined for C++ (since C++11).
    
  • 相关阅读:
    在mysql中,如何改变列声明.
    Field 'id' doesn't have a default value 原因
    secureCRT不能输入
    让工程科技造福人类、创造未来—在2014年国际工程科技大会上的主旨演讲
    jsp如何判断mysql数据库中是否已经存在添加的某条记录的方法
    mysql alter修改字段的长度 类型sql语句
    项目相关
    求职信模板
    PowerDesigner Constraint name uniqueness 错误
    PowerDesigner 创建表格及导出SQL语句
  • 原文地址:https://www.cnblogs.com/keviwu/p/7489876.html
Copyright © 2020-2023  润新知