Is it undefined behavior to take the address of an uninitialized pointer?

前端 未结 5 1186
猫巷女王i
猫巷女王i 2020-12-21 00:46

N1570 states that this is undefined behavior:

§J.2/1 The value of an object with automatic storage duration is used while it is indeterminate (6.2.4

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 01:41

    See this example

    char * ptr; 
    

    Since ptr is not pointing to any object, dereferencing it invokes undefined behavior. But when you pass its address to strtol, having syntax

    long int strtol(const char *nptr, char **endptr, int base);  
    

    in statement

    long parsed = strtol("11110111", &ptr, 2);   
    

    the endptr parameter of strtol is pointing to object ptr and derefeencing it will not invoke any UB.

提交回复
热议问题