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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:38:27+00:00 2026-06-05T07:38:27+00:00

I have two different data structs that should, in principle, have the same size,

  • 0

I have two different data structs that should, in principle, have the same size, and I’m wondering why they do not.

struct pix1 { 
    unsigned char r; 
    unsigned char g;
    unsigned char b; 
    unsigned char a; 
    unsigned char y[2]; 
 }; 

struct pix2 { 
    unsigned char r; 
    unsigned char g;
    unsigned char b; 
    unsigned char a; 
    unsigned short y; 
 }; 

Then, I group four of these pixels together, as such:

struct pix4 {
    pix1 pixels[4]; // or pix2 pixels[4]
    unsigned char mask;
};

…but it turns out that the size of such a grouping changes, according to sizeof(pix4), depending on whether I use pix1 or pix2. The individual sizeof(pix1) == sizeof(pix2), so I am confused as to why grouping quartets of pixels changes the size. I care because it is easier to write programs with the short than with the 2 unsigned chars, but it’s costing me 0.25 bytes per pixel.

I’m not sure if this architecture specific, as I haven’t tested on other types of machines. Could it be alignment? Is this something I need to worry about, or can I proceed with the short implementation?

Thanks for your help 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-05T07:38:32+00:00Added an answer on June 5, 2026 at 7:38 am

    The size of the structures is the same, but their alignment requirement is different.

    Alignment of a structure is the maximum of alignment of all its members. So pix1 has alignment 1, because it only has chars, but pix2 has alignment 2 from the short member. The alignment of pix4 then gets the alignment from the pixels member, so it’s 1 in the first and 2 in the second case.

    Now to ensure all members of an array are properly aligned, size of a structure is rounded up to next multiple of it’s alignment. In both cases the size of pixels is 24, but then there is 1-byte mask. In the first case the alignment is 1, so 25 is multiple of it and sizeof(pix4) is 25, but in the second the alignment is 2, so sizeof(pix4) has to be rounded up to next even number, 26.

    This is the same on all platforms.

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

Sidebar

Related Questions

I have two different DataContexts (SQL Databases) that have the same data, just with
I have two vector objects that contain different types of data that are ordered
I have a C# program that selects data from two different database files and
I have two pipes that both get different data at random points. What I
I have two different tables from which I need to pull data blogs which
I have a page with two Listviews (with two different data sources). I have
I have two different modules that need access to a single file (One will
I have two different projects and in one, I have a class that defines
I have two (or more) different databases, identically structured but each containing different data.
I have two constructors for an objects, which use two different sets of data.

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.