Arrays and std::array
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
An array is a fixed number of values of one type, stored side by side in one block of memory. Follow int a[4] = {3, 1, 4, 1}; into memory, see why a[i] is found by simple arithmetic (start + i × 4 bytes), change one element, add them all up with a loop, and see what happens past the end. Then meet std::array, the same row of values that knows its size and can check an index for you.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01int a[4] = {3, 1, 4, 1}; Which expression gives the last element?
Indexes count from 0, so an array of size 4 has the indexes 0, 1, 2 and 3. The last one is a[3]. a[4] is past the end.
02With 4-byte ints, how many bytes from the start of a is a[3]?
a[i] is at the start of a plus i × sizeof(int): 3 × 4 = 12 bytes. That simple sum is why any element can be reached in one step.
03What happens when a program writes a[4] = 7; for int a[4]?
A built-in array's size is fixed and C++ does not check a[i] while the program runs. Writing past the end is undefined behaviour: a crash, a changed neighbour, or nothing you can see.
04std::array<int, 4> b = {3, 1, 4, 1}; What does b.at(4) do?
at() checks the index before it touches memory. b[4], like a[4], is not checked.
05Which of these can a std::array do that a plain array cannot?
Both store their values side by side, and neither can grow. But a std::array behaves like a normal value: b2 = b; copies it, and passing it keeps its size. A plain array turns into a pointer to its first element when passed.
المسرد
- Array
- A fixed number of values of one type, stored side by side in one block of memory, such as int a[4].
- Element
- One value inside an array. a[2] is the element with index 2.
- Index
- An element's position, counted from 0. Valid indexes run from 0 to size - 1.
- sizeof
- An operator that gives the number of bytes a type or object takes, e.g. sizeof(int) is 4 on mainstream platforms.
- Undefined behaviour
- Code the C++ standard sets no rules for, such as reading or writing past the end of an array. Anything may happen.
- std::array
- A standard library class template, std::array<T, N> from <array>, holding N values of type T side by side. It knows its size and offers at() and size().
- std::out_of_range
- The exception that at() throws when the index is not below the size.
- Decay
- The automatic conversion of a plain array into a pointer to its first element, for example when it is passed to a function. The size is lost.
الملاحظات والمصادر
- sizeof(int) is 4 bytes on every mainstream platform (the ILP32, LP64 and LLP64 data models used by Linux, macOS and Windows). The C++ standard only promises at least 16 bits, so check with sizeof if it matters.
- The figure marks byte offsets from the start of the array (+0, +4, +8, +12) rather than real addresses: the real address of a changes from run to run.
- Undefined behaviour means the C++ standard sets no rules for what happens. Compilers may warn about a constant index such as a[4], and tools such as -fsanitize=address can catch it while testing, but nothing checks it in a normal build.
- std::array<T, N> is an aggregate that holds a plain T[N] as its only member, so it adds no bytes in practice (the test checks sizeof(std::array<int, 4>) == sizeof(int[4])). Its operator[] is unchecked, like a[i]; at() is the checked version.
- Passing a plain array to a function converts it to a pointer to its first element (array-to-pointer conversion), so the function no longer knows the size. std::array is passed like any object, by value or by reference, and keeps its size.
- The program on the card is real C++20. Build it with g++ -std=c++20 arrays.cpp -o arrays (or clang++) and run ./arrays: it prints 14, then 4.
- cppreference.com: Array declaration
- cppreference.com: Member access operators (built-in subscript)
- cppreference.com: Range-based for loop
- cppreference.com: Undefined behavior
- cppreference.com: std::array
- cppreference.com: std::array::at
- cppreference.com: Fundamental types (data models, sizeof(int))
- Standard C++ Foundation (isocpp.org): Container classes FAQ
- C++ Core Guidelines (isocpp.github.io): SL.con.1, prefer std::array or std::vector to a C array
اختبار يتاح بعد الخطوة الأخيرة.