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

  • SEARCH
  • Home
  • 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 42553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:21:22+00:00 2026-05-10T15:21:22+00:00

I’m writing an inner loop that needs to place struct s in contiguous storage.

  • 0

I’m writing an inner loop that needs to place structs in contiguous storage. I don’t know how many of these structs there will be ahead of time. My problem is that STL’s vector initializes its values to 0, so no matter what I do, I incur the cost of the initialization plus the cost of setting the struct‘s members to their values.

Is there any way to prevent the initialization, or is there an STL-like container out there with resizeable contiguous storage and uninitialized elements?

(I’m certain that this part of the code needs to be optimized, and I’m certain that the initialization is a significant cost.)

Also, see my comments below for a clarification about when the initialization occurs.

SOME CODE:

void GetsCalledALot(int* data1, int* data2, int count) {     int mvSize = memberVector.size()     memberVector.resize(mvSize + count); // causes 0-initialization      for (int i = 0; i < count; ++i) {         memberVector[mvSize + i].d1 = data1[i];         memberVector[mvSize + i].d2 = data2[i];     } } 
  • 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. 2026-05-10T15:21:22+00:00Added an answer on May 10, 2026 at 3:21 pm

    std::vector must initialize the values in the array somehow, which means some constructor (or copy-constructor) must be called. The behavior of vector (or any container class) is undefined if you were to access the uninitialized section of the array as if it were initialized.

    The best way is to use reserve() and push_back(), so that the copy-constructor is used, avoiding default-construction.

    Using your example code:

    struct YourData {     int d1;     int d2;     YourData(int v1, int v2) : d1(v1), d2(v2) {} };  std::vector<YourData> memberVector;  void GetsCalledALot(int* data1, int* data2, int count) {     int mvSize = memberVector.size();      // Does not initialize the extra elements     memberVector.reserve(mvSize + count);      // Note: consider using std::generate_n or std::copy instead of this loop.     for (int i = 0; i < count; ++i) {         // Copy construct using a temporary.         memberVector.push_back(YourData(data1[i], data2[i]));     } } 

    The only problem with calling reserve() (or resize()) like this is that you may end up invoking the copy-constructor more often than you need to. If you can make a good prediction as to the final size of the array, it’s better to reserve() the space once at the beginning. If you don’t know the final size though, at least the number of copies will be minimal on average.

    In the current version of C++, the inner loop is a bit inefficient as a temporary value is constructed on the stack, copy-constructed to the vectors memory, and finally the temporary is destroyed. However the next version of C++ has a feature called R-Value references (T&&) which will help.

    The interface supplied by std::vector does not allow for another option, which is to use some factory-like class to construct values other than the default. Here is a rough example of what this pattern would look like implemented in C++:

    template <typename T> class my_vector_replacement {      // ...      template <typename F>     my_vector::push_back_using_factory(F factory) {         // ... check size of array, and resize if needed.          // Copy construct using placement new,         new(arrayData+end) T(factory())         end += sizeof(T);     }      char* arrayData;     size_t end; // Of initialized data in arrayData };  // One of many possible implementations struct MyFactory {     MyFactory(int* p1, int* p2) : d1(p1), d2(p2) {}     YourData operator()() const {         return YourData(*d1,*d2);     }     int* d1;     int* d2; };  void GetsCalledALot(int* data1, int* data2, int count) {     // ... Still will need the same call to a reserve() type function.      // Note: consider using std::generate_n or std::copy instead of this loop.     for (int i = 0; i < count; ++i) {         // Copy construct using a factory         memberVector.push_back_using_factory(MyFactory(data1+i, data2+i));     } } 

    Doing this does mean you have to create your own vector class. In this case it also complicates what should have been a simple example. But there may be times where using a factory function like this is better, for instance if the insert is conditional on some other value, and you would have to otherwise unconditionally construct some expensive temporary even if it wasn’t actually needed.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Consider these filenames: C:\temp\file.txt - This is a path, an… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer Your probably going to have to do this with the… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer Place a 16x16 favicon.ico file in your websites root directory.… May 11, 2026 at 11:43 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.