Pointers
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
Every variable lives in memory at an address, and a pointer is a variable whose value is one of those addresses. Follow an eleven-line program as &x puts x's address into a pointer p, *p = 8 writes through p into x, p = &y makes it point somewhere else, and nullptr leaves it pointing nowhere, which you must never follow.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01After int x = 5; int* p = &x; what is the value stored in p?
&x is the address of x, and that address is what p holds. p does not hold 5: *p (following p) gives 5.
02Continuing from int* p = &x; which line changes x to 8?
*p means the object p points to, which is x. So *p = 8 writes 8 into x's memory. p = 8 tries to change the address itself, and does not compile, because 8 is an int, not an address.
03p points to x. Then the program runs p = &y; What happens to x?
Without a star, p = &y changes p itself: it now holds y's address. x keeps its value; it is just no longer what p points to.
04What is the size of an int* on a usual 64-bit system (Linux, macOS or Windows)?
A pointer holds an address, and on 64-bit systems addresses are 64 bits, so sizeof(int*) is 8, while sizeof(int) is usually 4. The standard leaves these sizes to the platform.
05p is nullptr. What does *p = 1; do?
A null pointer points at no object, so following it is undefined behaviour. The compiler often cannot see it coming, so check first: if (p) { *p = 1; }.
المسرد
- Address
- The number of a place in memory. Every byte has one; an object's address is the address of its first byte.
- Pointer
- A variable whose value is an address. int* p is a pointer to an int.
- & (address-of)
- Placed before a variable, as in &x, it gives that variable's address.
- * (indirection, or dereference)
- Placed before a pointer, as in *p, it follows the address to the object there, to read it or write it.
- nullptr
- The null pointer literal (C++11 and later): a pointer value that points at no object.
- Undefined behaviour
- Code the C++ standard sets no rules for, such as following a null pointer. Anything may happen; often a crash.
- sizeof
- An operator that gives the size of a type or object in bytes, such as sizeof(int*).
الملاحظات والمصادر
- The addresses on the figure are made up and short so they fit. Real addresses are longer (on a 64-bit machine, something like 0x16fdff0bc), and most systems place a program at a different address every run. The compiler, not you, decides where x, y and p go and in what order; they may not sit side by side.
- Sizes depend on the platform. The C++ standard only says int is at least 16 bits. On today's usual 64-bit systems (Linux, macOS, Windows) an int is 4 bytes and a pointer is 8; check with sizeof(int) and sizeof(int*).
- x, y and p are local variables of main, so they live in main's stack frame. Pointers can also point to objects on the heap, which comes later in this module.
- Undefined behaviour means the C++ standard sets no rules for what happens. Following a null pointer usually stops the program with a crash (a segmentation fault on Linux and macOS, an access violation on Windows), but an optimising compiler may also do something stranger.
- The program on the card is real, standard C++11 or later. Build it with g++ -std=c++20 pointers.cpp -o pointers (or clang++), run ./pointers and it prints 8, then 7.
- cppreference.com: Pointer declaration
- cppreference.com: Member access operators (built-in & and *)
- cppreference.com: nullptr, the pointer literal
- cppreference.com: Object (size, alignment, address)
- cppreference.com: Fundamental types (sizes and data models)
- cppreference.com: Undefined behavior
- C++ Core Guidelines (isocpp.org): ES.47 Use nullptr rather than 0 or NULL
- Standard C++ Foundation (isocpp.org): FAQ, References
- Bjarne Stroustrup: A Tour of C++ (3rd ed.), Pointers, Arrays, and References
اختبار يتاح بعد الخطوة الأخيرة.