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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:05:08+00:00 2026-06-08T23:05:08+00:00

I have an std::vector<SomeType> , whereas SomeType is a struct instantiated from various template

  • 0

I have an std::vector<SomeType>, whereas SomeType is a struct instantiated from various template parameters.

Eventually in my specific case it turns out to be a large structure (about 1MB). Allocating such a structure on the stack (i.e. using an automatic variable of this type) immediately leads to the stack overflow. But since std::vector allocates the memory on the heap – there should be no problem.

Surprisingly there is a problem with this. Specifically, the problem is with the initialization. I do the following:

std::vector<SomeType> myVec;
// ...
myVec.resize(N);
for (size_t i = 0; i < N; i++)
{
    SomeType& x = myVec[i];
    // initialize it
}

I get a stack overflow exception in myVec.resize(). Stepping inside resize() with the debugger revealed that if resize() causes the vector to grow – besides of allocating the memory it also initializes the new elements by “default values”.

The “default value” is obtained by creating an automatic variable of the given type, using default (i.e. empty) constructor, and assigning the new element to it.

I wonder if there’s a way to overcome this. I mean, tell std::vector not to initialize the new elements. But I’d like to achieve this without generating unneeded code.

I can think of using a vector of (smart/shared/scoped/unique) pointers to type. Or alternatively use push_back whereas each new element is allocated on heap. But all this inevitable involves extra code. Which is not justified.

Is there a way to achieve what I need? Whereas:

  • still using std::vector<SomeType>
  • No redundant heap allocations

Thanks in advance

  • 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-06-08T23:05:09+00:00Added an answer on June 8, 2026 at 11:05 pm

    In C++03 it’s impossible:

    • Vectors cannot contain uninitialized elements
    • Vectors only know how to initialize elements by copying

    Therefore you need an element to copy from. It doesn’t fit on the stack (ruling out the default argument) and you don’t want to explicitly put it elsewhere, so you’re out of luck.

    In C++11 there are new means of initializing elements in a container, and for example the size_t constructor no longer takes an extra default argument. Instead it value-initializes each element, which is probably what you want.

    So in C++11, the answer is std::vector<SomeType> myVec(N);. Perhaps you could check whether your compiler has a C++11 mode that you could use to compile your code. Of course, migrating to C++11 isn’t completely trivial.

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

Sidebar

Related Questions

Sitation: overview: I have something like this: std::vector<SomeType> values; std::vector<int> indexes; struct Range{ int
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
i have a std::vector, namely vector<vector<vector> > > mdata; i want pass data from
I have a std::vector and I want to check a specific attribute of each
I have: struct DoubleVec { std::vector<double> data; }; DoubleVec operator+(const DoubleVec& lhs, const DoubleVec&
I have a std::vector<Word> data that is off of the struct below: struct Word
I have a std::vector of some class of the form class A{ public: A():i(someNumber){}
I have an std::vector of handle objects. I have to wait on these handle

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.