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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:15:02+00:00 2026-05-27T08:15:02+00:00

In C/C++ suppose I define a simple struct named test as follows. struct test

  • 0

In C/C++ suppose I define a simple struct named test as follows.

struct test
{
   double height;
   int    age;
   char   gender;
}

For a specific instance of this struct say test A are A.height, A.age, A.gender contiguous
in memory?

More generally, how do the layouts in memory for a Structure of Arrays and an Array of structures look like? A picture would be really helpful.

  • 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-27T08:15:03+00:00Added an answer on May 27, 2026 at 8:15 am

    They will not necessarily be contiguous in memory. This is due to struct padding.

    However, in your particular case, it may very well be contiguous. But if you changed the order to something like this:

    struct test
    {
        char   gender;
        int    age;
        double height;
    }
    

    then they most likely will not be. However, in your particular case, you will still likely get padding after gender, to realign the struct to 8 bytes.


    The difference between SoA (Struct of Arrays) and AoS (Array of Structs) would be like this:

    SoA:

    -----------------------------------------------------------------------------------
    | double | double | double | *pad* | int | int | int | *pad* | char | char | char |
    -----------------------------------------------------------------------------------
    

    AoS:

    -----------------------------------------------------------------------------------
    | double | int | char | *pad* | double | int | char | *pad* | double | int | char |
    -----------------------------------------------------------------------------------
    

    Note that AoS pads within each struct. While SoA pads between the arrays.

    These have the following trade-offs:

    1. AoS tends to be more readable to the programmer as each “object” is kept together.
    2. AoS may have better cache locality if all the members of the struct are accessed together.
    3. SoA could potentially be more efficient since grouping same datatypes together sometimes exposes vectorization.
    4. In many cases SoA uses less memory because padding is only between arrays rather than between every struct.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I define a union like this: #include <stdio.h> int main() { union u
Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int
Suppose that we define an interface like this: interface Hashable { int hash(); }
My simple first Palette is suppose to: Append my packages Path to $Path ActionMenu[test,{The
Suppose I have #define foo in various header files. It may expand to some
Suppose I have a list of #define s in a header file for an
I understand that we need to create MXML file to define a view. Suppose
Here's some simple code that, for each argument specified, will add specific get/set methods
Consider the following simple map: class MyCoolMap : public unordered_map<const char *, const char
uint ci = 0; struct S { uint i; this(int x) { i =

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.