Is the memory in std::array contiguous? Is the following valid/good practice?
std::array<type1,Num> arr = //initialize value
type1 * ptr = &arr[0];
Could I then pass ptr to functions expecting a c-style array?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, it is contiguous, as it is basically (and actually) a
type arr[10];, but with STL like interface. It also doesn’t decay to a pointer on the slightest provocation.You can safely pass
&arr[0]to a function expecting a C-style array, that’s the design goal of it. To use it with the STL algorithms however, just use thebeginandendfunctions:For the language lawyer part,
§23.3.2.1 [array.overview] p1:And
§23.3.2.1 [array.overview] p2:Also, in
p3, listing the members ofstd::array: