在C ++中如何以及如何不能使用空指针





对于某些人来说,这个琐碎的问题已经使人大吃一惊,但是我们举了7个示例,并尝试使用标准来解释它们的行为:



struct A {
    int data_mem;
    void non_static_mem_fn() {}
    static void static_mem_fn() {}
};

void foo(int) {}

A* p{nullptr};

/*1*/ *p;
/*2*/ foo((*p, 5));                     
/*3*/ A a{*p};
/*4*/ p->data_mem;
/*5*/ int b{p->data_mem};
/*6*/ p->non_static_mem_fn();
/*7*/ p->static_mem_fn();


一个明显但重要的细节:用空指针初始化的p无法指向类型A的对象,因为它的值不同于指向类型为conv.ptr#1的任何对象的指针的值



Disclaimer: . dev.to, .



1



struct A {
    int data_mem;
    void non_static_mem_fn() {}
    static void static_mem_fn() {}
};

void foo(int) {}

A* p{nullptr};




*p;


(expression statement, stmt.expr#1), *p , , , . * expr.unary.op#1 , (indirection), l-, , . , , . .



, basic.stc#4, , (indirection through an invalid pointer value) . , , basic.compound#3.4, , — .



dcl.ref#5, , «the only way to create such a reference would be to bind it to the “object” obtained by indirection through a null pointer, which causes undefined behavior», .. — «», , . , «» (to bind), , , dcl.ref#5.



, , Core Working Group . , CWG ( drafting), , l- r-. « CWG» , , , 7. CWG.



. (N2176, 6.5.3.2 104), , , .



2



struct A {
    int data_mem;
    void non_static_mem_fn() {}
    static void static_mem_fn() {}
};

void foo(int) {}

A* p{nullptr};




foo((*p, 5));  


foo, , «». , , , , 1 (expr.comma#1). , .



3



struct A {
    int data_mem;
    void non_static_mem_fn() {}
    static void static_mem_fn() {}
};

void foo(int) {}

A* p{nullptr};




A a{*p};


a , , , const A& , (dcl.ref#5). .



4



struct A {
    int data_mem;
    void non_static_mem_fn() {}
    static void static_mem_fn() {}
};

void foo(int) {}

A* p{nullptr};




p->data_mem;


(*(p)).data_mem expr.ref#2, (designate) , (expr.ref#6.2). 1 , , , basic.lookup.qual#1, , to refer to designate , expr.ref. , , (. ).



5



struct A {
    int data_mem;
    void non_static_mem_fn() {}
    static void static_mem_fn() {}
};

void foo(int) {}

A* p{nullptr};




int b{p->data_mem};


, int. pr-, (basic.lval#1.2). int, (conv.lval#3.4), , basic.lval#11 .



6



struct A {
    int data_mem;
    void non_static_mem_fn() {}
    static void static_mem_fn() {}
};

void foo(int) {}

A* p{nullptr};




p->non_static_mem_fn();


class.mfct.non-static#1 , - , ( ), - . «» — «may be» /, . , .



7



struct A {
    int data_mem;
    void non_static_mem_fn() {}
    static void static_mem_fn() {}
};

void foo(int) {}

A* p{nullptr};




p->static_mem_fn();


1, Core Working Group . , 59, -> , .



constexpr



(expr.const#5), . , . , constexpr , , GCC, MSVC : godbolt



:



#





GCC 10.1

Clang 10

MSVC 19.24

1

*p;

+

+

+

+

2

foo((*p, 5));

+

+

+

+

3

A a{*p};

4

p->data_mem;

+

+

5

int b{p->data_mem};

6

p->non_static_mem_fn();

+

+

7

p->static_mem_fn();

+

+

+

+



6 4. , 1.



感谢您与我们在一起,直到最后了解C ++中空指针的冒险!:-)通常,我们在Habré上共享真实项目中的代码片段,用于开发电子产品嵌入式软件,但是这次我们只对“哲学”问题感兴趣,因此这些示例是综合性的。



如果您分享我们对C ++的热爱,请在评论中分享您“痛苦”的话题。 




All Articles