zoukankan      html  css  js  c++  java
  • 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).
    
  • 相关阅读:
    extract numberic from text
    str.rfind("//")
    solr入门心得
    深挖洞,广积粮,缓称王
    ubuntu 查看文件夹大小

    关于托运
    iconv转换文件编码
    小毛小毛你的心态
    广积粮,高筑墙,缓称王
  • 原文地址:https://www.cnblogs.com/keviwu/p/7489876.html
Copyright © 2011-2022 走看看