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 3611272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:51:22+00:00 2026-05-18T21:51:22+00:00

I need to have a fixed-size array of elements and to call on them

  • 0

I need to have a fixed-size array of elements and to call on them functions that require to know about how they’re placed in memory, in particular:

  • functions like glVertexPointer, that needs to know where the vertices are, how distant they are one from the other and so on. In my case vertices would be members of the elements to store.

  • to get the index of an element within this array, I’d prefer to avoid having an index field within my elements, but would rather play with pointers arithmetic (ie: index of Element *x will be x - & array[0]) — btw, this sounds dirty to me: is it good practice or should I do something else?


Is it safe to use std::vector for this?

Something makes me think that an std::array would be more appropriate but:

  • Constructor and destructor for my structure will be rarely called: I don’t mind about such overhead.

  • I’m going to set the std::vector capacity to size I need (the size that would use for an std::array, thus won’t take any overhead due to sporadic reallocation.

  • I don’t mind a little space overhead for std::vector‘s internal structure.

  • I could use the ability to resize the vector (or better: to have a size chosen during setup), and I think there’s no way to do this with std::array, since its size is a template parameter (that’s too bad: I could do that even with an old C-like array, just dynamically allocating it on the heap).


If std::vector is fine for my purpose I’d like to know into details if it will have some runtime overhead with respect to std::array (or to a plain C array):

I know that it’ll call the default constructor for any element once I increase its size (but I guess this won’t cost anything if my data has got an empty default constructor?), same for destructor. Anything else?

  • 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-18T21:51:22+00:00Added an answer on May 18, 2026 at 9:51 pm

    Vectors are guaranteed to have all elements in contigous memory, so it is safe to use in your scenario. There can be a small performance hit compared to c-style arrays, for instance due to index validations done by the vector implementation. In most cases, the performance is determined by something else though, so I wouldn’t worry about that until actual measurements of performance show that this a real problem.

    As pointed out by others, make sure that you don’t reallocate the vector while you are making use of the data stored in it if you are using pointers to elements (or iterators) to access it.

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

Sidebar

Related Questions

I have a few models that need to have custom find conditions placed on
I have a fixed-length data file need to persistence into database. I use a
I have a fixed width DIV containing a table with many columns, and need
I need to have multiple forms in the same webpage, all of them POSTing
We have need for a rating system in a project we are working on,
I have need to select a number of 'master' rows from a table, also
I need to have a summary field in each page of the report and
I need to have a thread signal another if the user wishes to interrupt
I need to have a script read the files coming in and check information
I need to have a working web browser in a fullscreen Direct3D application. For

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.