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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:52:13+00:00 2026-06-15T00:52:13+00:00

struct bar { int32 a; // 4 bytes int32 b; // 4 bytes vector<int>

  • 0
struct bar {
  int32 a;    // 4 bytes
  int32 b;    // 4 bytes
  vector<int> c;      // something like 8 or 16 bytes?
}

When I do sizeof(bar) it gives 32 bytes!
Any way I could align it to be something smaller? Any potential
penalty for that? I am not quite sure about the vector part…

  • 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-15T00:52:14+00:00Added an answer on June 15, 2026 at 12:52 am

    The normal implementation of std::vector<T, A> is to have a pointer to have three pointers plus, for stateful allocators, whatever it takes to maintain the allocator:

    1. A pointer to the start of the vector.
    2. A pointer to the end of the vector or the number of elements in the vector.
    3. A pointer to the end of the allocated memory or the number of elements allocated.

    When putting this into a type, it will likely be padded to 4 words to improve access times. When you combine a vector like this with two more data members, you’ll naturally get a type which occupies 8 words, three of which are to align the type for fast access.

    If you really care about the size of your structs containing vector because the vector is normally empty and only rarely contains data, you’d probably prefer an implementation with a different layout: The vector would still need to start the information mentioned above but it could start the data with the allocated buffer: When allocating memory, the vector would allocate extra space to prepend its administration information to the actual values and store a pointer to the allocated data (probably to the first element which, however, makes recovery of the administration information not portable).

    Using a representation just described, the size of the vector would be one word and the total size of your struct would be three words plus padding, i.e., probably 4 words. The better size is traded for a small overhead when the size or the capacity of the vector is needed (e.g., when using begin() and `end()).

    In general, the size of the vectors tends not to matter because the actual memory consumption of the vectors is down to the elements in the vector.

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

Sidebar

Related Questions

Is something like the code below valid? struct foo { int a; int b[];
Say I have an object: struct Foo { int bar_; Foo(int bar) bar_(bar) {}
In the ARM ABI documentation I come across functions defined like: __value_in_regs struct bar
This works fine: #include <iostream> #include <map> using namespace std; struct Bar { int
Consider this smallest example (I could think of): struct Bar; struct Foo { Bar*
If I have an object like this: struct Bar { std::string const& property(); };
What could this possibly mean in C++11? struct : bar {} foo {};
I got a structure like this: struct bar { char x; char *y; };
Say i have a data structure like this: type Foo struct { Bar []struct
Suppose I have: struct Magic { Magic(Foo* foo); Magic(Bar* bar); }; Is there a

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.