简体   繁体   中英

About reinterpret_cast and pointer

I am learning C and C++. In this program:

#include <iostream>

int main() {

    const int g = 10;
    int * k = reinterpret_cast <int *> (20000); //ok
    std::cout << "k : " << k  << std::endl;
    std::cout << "* k : " << * k << std::endl;

    * k = g;
    std::cout << "* k : " << *k << std::endl;

    return 0;
}

./converForTyEn
k : 0x4e20 Segmentation fault (core dumped)

I expect 20000 and then 10. But it seems that the error is "means that you tried to access memory that you do not have access to." (Erci Finn). Thank for help.

The working draft of the standard (N4713) states regarding the usage of

8.5.1.10 Reinterpret cast
...
5. A value of integral type or enumeration type can be explicitly converted to a pointer . A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation-defined. [ Note: Except as described in 6.6.4.4.3, the result of such a conversion will not be a safely-derived pointer value . —end note ]

And:

6.6.4.4.3 Safely-derived pointers [basic.stc.dynamic.safety]
...
2. A pointer value is a safely-derived pointer to a dynamic object only if it has an object pointer type and it is one of the following :
(2.1) — the value returned by a call to the C++ standard library implementation of ::operator new(std::size_t) or ::operator new(std::size_t, std::align_val_t);
(2.2) — the result of taking the address of an object (or one of its subobjects) designated by an lvalue resulting from indirection through a safely-derived pointer value;
(2.3) — the result of well-defined pointer arithmetic using a safely-derived pointer value;
(2.4) — the result of a well-defined pointer conversion of a safely-derived pointer value;
(2.5) — the result of a reinterpret_cast of a safely-derived pointer value;
(2.6) — the result of a reinterpret_cast of an integer representation of a safely-derived pointer value;
(2.7) — the value of an object whose value was copied from a traceable pointer object, where at the time of the copy the source object contained a copy of a safely-derived pointer value.

Since you are using reinterpret_cast on an integer literal ( 20000 ), the result of the conversion is not a safely-derived pointer value. Attempting to dereference such a pointer value results in undefined behavior.

A pointer must point to valid memory to be used. In your case you set it to an arbitrary memory location, 20000.

An example of valid pointer usage would be

int x = 42;
int *px = &x;

This puts the address of x into px .

std::cout << px;

Would output the address.

std::cout << *px;

Would output the value of x .

Your code invokes Undefined Behavior (UB), and a likely output would be:

k : 0x4e20
Segmentation fault

where the first line is the address of k . Then, in this line of code:

std::cout << "* k : " << * k << std::endl;

you are trying to access this address, which is out of the segment of your program, thus causing a segmentation fault.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM