Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6119269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:31:59+00:00 2026-05-23T15:31:59+00:00

Is the memory in std::array contiguous? Is the following valid/good practice? std::array<type1,Num> arr =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:32:00+00:00Added an answer on May 23, 2026 at 3:32 pm

    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 the begin and end functions:

    // either members
    std::sort(arr.begin(), arr.end());
    // or free from <iterator>
    std::sort(std::begin(arr), std::end(arr));
    

    For the language lawyer part, §23.3.2.1 [array.overview] p1:

    The header <array> defines a class template for storing fixed-size sequences of objects. An array supports random access iterators. An instance of array<T, N> stores N elements of type T, so that size() == N is an invariant. The elements of an array are stored contiguously, meaning that if a is an array<T, N> then it obeys the identity &a[n] == &a[0] + n for all 0 <= n < N.

    And §23.3.2.1 [array.overview] p2:

    An array is an aggregate (8.5.1) that can be initialized with the syntax

    • array<T, N> a = { initializer-list };

    Also, in p3, listing the members of std::array:

    T elems[N]; // exposition only
    [ Note: The member variable elems is shown for exposition only, to emphasize that array is a class aggregate. The name elems is not part of array’s interface. —end note ]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
is it possible to use std::fill to initialize an array of non-POD types? The
How can I access the contiguous memory buffer used within a std::vector so I
I have a static std::vector in a class. When I use Microsoft's memory leak
// dll #include <memory> __declspec(dllexport) std::auto_ptr<int> get(); __declspec(dllexport) std::auto_ptr<int> get() { return std::auto_ptr<int>(new int());
What memory leak detectors have people had a good experience with? Here is a
Possible Duplicate: Are std::vector elements guaranteed to be contiguous? does std::vector always contain the
Is following array initialization correct? I guess it is, but i'm not really sure
I wrote the following heap debugger in order to demonstrate memory leaks, double deletes
Benefits of C++11 std::array s when programming have been explained by experts, but there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.