标签云

微信群

扫码加入我们

WeChat QR Code

C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector<T> since C++98 and std::array<T, n> since C++11), so the need for arrays does not arise quite as often as it does in C. However, when you read legacy code or interact with a library written in C, you should have a firm grasp on how arrays work.This FAQ is split into five parts:arrays on the type level and accessing elementsarray creation and initializationassignment and parameter passingmultidimensional arrays and arrays of pointerscommon pitfalls when using arraysIf you feel something important is missing in this FAQ, write an answer and link it here as an additional part.In the following text, "array" means "C array", not the class template std::array. Basic knowledge of the C declarator syntax is assumed. Note that the manual usage of new and delete as demonstrated below is extremely dangerous in the face of exceptions, but that is the topic of another FAQ.(Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)


They would be even better if the pointers always pointed to the begining instead of somewhere in the middle of their target though...

2019年06月26日59分23秒

You should use the STL Vector because it provides you greater flexibility.

2019年06月25日59分23秒

With the combined availability of std::arrays, std::vectors and gsl::spans - I would frankly expect an FAQ on how to use arrays in C++ to say "By now, you can start considering just, well, not using them."

2019年06月25日59分23秒

Cases where array doesn't decay into a pointer is illustrated here for reference.

2019年06月25日59分23秒

fredoverflow In the Access or Ranges part it might be worth mentioning that C-arrays work with C++11 range-based for loops.

2019年06月25日59分23秒

Might be worth it to add a note that even tho in void foo(int a[3]) a does look like one is passing the array by value, modifying a inside of foo will modify the original array. This should be clear because arrays cannot be copied, but it might be worth it to reinforce that.

2019年06月25日59分23秒

The deprecation of static usage in namespace scope was removed in C++11.

2019年06月25日59分23秒

Because new is am operator, it certainly could return the allcated array by reference. There's just no point to it...

2019年06月25日59分23秒

Deduplicator No it could not, because historically, new is a lot older than references.

2019年06月26日59分23秒

FredOverflow: So there is a reason it could not return a reference, it's just completely different from the written explanation.

2019年06月25日59分23秒

Deduplicator I don't think a reference to an array of unknown bounds exists. At least g++ refuses to compile int a[10]; int (&r)[] = a;

2019年06月26日59分23秒

+1 Great C coding test: I have spend 15 minutes on VC++ 10.0 and GCC 4.1.2 trying to fix the Segmentation fault... I finally found/understood after reading your explanations! Please write your §5.2 section :-) Cheers

2019年06月25日59分23秒

Good. One nit - the return type for countOf should be size_t instead of ptrdiff_t. It's probably worth mentioning that in C++11/14 it should be constexpr and noexcept.

2019年06月25日59分23秒

Ricky65: Thanks for mentioning C++11 considerations. Support for these features has been late in coming for Visual C++. Regarding size_t, that has no advantages that I know of for modern platforms, but it has a number of problems due to the implicit type conversion rules of C and C++. That is, ptrdiff_t is used very intentionally, to avoid the problems with size_t. One should however be aware that g++ has a problem with matching array size to template parameter unless it's size_t (I don't think this compiler-specific problem with non-size_t is important, but YMMV).

2019年06月25日59分23秒

Alf. In the Standard Working Draft (N3936) 8.3.4 I read - The bound of an array is... "a converted constant expression of type std::size_t and its value shall be greater than zero".

2019年06月25日59分23秒

Ricky: If you're referring to the inconsistency, this statement is not there in the current C++11 standard so it's difficult to guess the context, but the contradiction (a dynamically allocated array can be of bound 0, per C++11 §5.3.4/7) will probably not end up in C++14. Drafts are just that: drafts. If you're instead asking about what "its" refers to, it refers to the original expression, not the converted one. If on the third hand you mention this because you think that maybe such a sentence means that one should use size_t to denote sizes of arrays, no of course it doesn't.

2019年06月25日59分23秒