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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:10:31+00:00 2026-05-25T13:10:31+00:00

I am trying the following code: struct _Struct2 { void *ptr; double dval; };

  • 0

I am trying the following code:

struct _Struct2
{
    void   *ptr;
    double dval;
};

struct _Struct
{
    float fval;
    int   ival;
    std::vector<_Struct2>   data;
};

std::vector<_Struct>    vec;

int main()
{
    vec.resize( 9 );
    for ( int i = 0; i < vec.size(); i++ )
    {
        _Struct &elem = vec[i];
        int     len = elem.data.size(); // elem.data is [0]()
    }
}

The resize(9) should allocate 9 elements of type _Struct and it works. But every element of type _Struct is not initialized, especially the data element which is another std::vector. I would like it to be initialized to the empty std::vector. Do I have to do that manually? I thought that the resize() method would have called the default constructor of every _Struct element. Thx

Ps. The names of the structs used here are just the first things that come to my mind. This is just an example. My Visual Studio tells me that elem.data corresponds in the debug view to [0]().

Ps. Forget the [0]().

  • 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-25T13:10:32+00:00Added an answer on May 25, 2026 at 1:10 pm

    No it doesn’t call default element constructor. std::vector never calls default constructors internally (it does in C++11, but not in earlier versions of the specification).

    The full signature for vector::resize looks as follows

    void resize(size_type sz, T c = T());
    

    I.e. it has a second parameter (with default argument value). That second parameter is then used as a “source” object to initialize new elements by copy-constructor.

    In other words, your call to resize is actually equivalent to

    vec.resize( 9, _Struct() );
    

    meaning that it is you who called the default constructor when you supplied that “source” object to vector::resize, even though you didn’t notice that.

    But every element of type _Struct is not initialized, especially the
    data element, which is another std::vector.

    Huh? “Not initialized”? I don’t know what that is supposed to mean, considering that in your sample code every new element created by resize is perfectly initialized as described above: it is copy-initialized from a _Struct() element you implicitly supplied to resize as the second argument. Each _Struct::fval and _Struct::ival is zero, and each _Struct::data is an empty vector.

    (In the original C++98 _Struct::fval and _Struct::ival will remain uninitialized, since pre-TC1 C++98 didn’t support value-initialization. But _Struct::data will be initialized to an empty vector even in the original C++98).

    I would like it to be initialized to the empty std::vector.

    Every _Struct::data vector is already initialized as an empty vector. What made you believe that it isn’t?

    P.S. Names that begin with _ followed by an uppercase letter are reserved by the implementation. You are not allowed to use them.

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

Sidebar

Related Questions

I am trying the following code: <?php $link = mysql_connect('localhost', 'root', 'geheim'); if (!$link)
I'm trying the following code http://code.google.com/apis/ajax/playground/#change_the_playing_video It works well when runned from the playground
I'm trying the following code to execute a search and it's not working. On
I was trying out the following code which actually saves the pdf file to
When I am trying to execute following code to email the contact form details,
The following code is trying to remove any duplicate characters in a string. I'm
When trying to compile the following code, I am getting a warning that line
I'm trying to use the following code but it's returning the wrong day of
I'm trying to get the following code working: string url = String.Format(@SOMEURL); string user
I am trying to use the following code to write data into an excel

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.