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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:35:46+00:00 2026-06-17T15:35:46+00:00

You can create a C++ structure with an array within: struct foo{ int bar[42];

  • 0

You can create a C++ structure with an array within:

struct foo{
    int bar[42];
};

What I would like to do is:

struct foo{
    std::vector<int> bar(42);
};

Of course this doesn’t compile, but you get the intent. I’m aware of .reserve() and the like,
but I would like to have the space already allocated when declaring a foo.

The reason is that I’m supposed to alter a rather complicated Perl script which is generating C code with arrays within structs. These arrays should be replaced by std::vectors. The script subsequently initializes the arrays depending on an XML file and I would rather not mess around with push_back in the script since the structures are deeply nested (structs of arrays and arrays of structs). The sizes of the arrays do of course vary.

Thanks for your suggestions.

  • 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-17T15:35:48+00:00Added an answer on June 17, 2026 at 3:35 pm

    Use a constructor:

    struct foo{
        std::vector<int> bar;
        foo() : bar(32) {}
    };
    

    Memory for the vector will automatically be allocated when creating an instance. The initialization list initializes members before the constructor body executes.

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

Sidebar

Related Questions

I would like to create a structure within javascript. I have a pair of
In matlab, I can create a structure array (struct) by doing the following. person.name
I'd like to take an array with this structure: array 'Alabama' => array 0
Given an arbitrarily nested data structure, how can I create a new data structure
Is there a pattern, Xml structure, architecture technique we can use to create simple
I can create and use dynamic two dimensional array in Fortran (in 77 standard).
I can create database manually by going to cpanel. But I'd like to create
I'm trying to create a structure array which links input strings to classes as
In Java, how can I create an array where each element is a list
I have created a structure and populated an array with variables that can be

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.