Wie Sie können und wie Sie keinen Nullzeiger in C ++ verwenden können





Für einige hat diese triviale Frage bereits die Zähne auf den Kopf gestellt, aber wir haben 7 Beispiele genommen und versucht, ihr Verhalten anhand des Standards zu erklären:



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();


Ein offensichtliches, aber wichtiges Detail: p , das mit einem Nullzeiger initialisiert wurde, kann nicht auf ein Objekt vom Typ A zeigen, da sich sein Wert vom Wert eines Zeigers auf ein Objekt vom Typ A conv.ptr # 1 unterscheidet .



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.



Vielen Dank, dass Sie bis zum Ende bei uns bleiben, um die Abenteuer des Nullzeigers in C ++ zu verfolgen! :-) Normalerweise teilen wir Habré-Code aus realen Projekten zur Entwicklung eingebetteter Software für die Elektronik, aber diesmal waren wir an rein "philosophischen" Fragen interessiert, daher sind die Beispiele synthetisch.



Wenn Sie unsere Liebe zur Kontroverse in C ++ teilen, teilen Sie Ihre "schmerzhaften" in den Kommentaren. 




All Articles