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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:21:28+00:00 2026-06-14T13:21:28+00:00

Possible Duplicate: C struct sizes inconsistence For the following program, i’d like to obtain

  • 0

Possible Duplicate:
C struct sizes inconsistence

For the following program, i’d like to obtain the size of a struct. However, it turns the size of it is 12 rather than 4*4=16. Does it means that each element can align to a different pad number? like int with 4 and short with 2, but in this case char should have 1.

Thx.

#include <stdio.h>

struct test{
int a;
char b;
short c;
int d;

};

struct test A={1,2,3,4};

int main()
{

    printf("0X%08X\n",&A.a);
    printf("0X%08X\n",&A.b);
    printf("0X%08X\n",&A.c);
    printf("0X%08X\n",&A.d);
    printf("%d\n",sizeof(A));

}

And the result is:

0X00424A30
0X00424A34
0X00424A36
0X00424A38
12
  • 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-14T13:21:30+00:00Added an answer on June 14, 2026 at 1:21 pm

    Yes, every type don’t have the same alignment. Each of your variable shall be aligned correctly, ie their addresses shall be a multiple of a certain size. The usual rule (for Intel and AMD, among other) is that every data type is aligned by its own size. Assuming x86 architecture, it seems to be right here:

    • 0X00424A30: first address of the structure.
    • 0X00424A34: 4 bytes (maybe sizeof(int)) after the first member. char requires an alignment of 1, so it doesn’t need padding here.
    • 0X00424A36: 2 bytes after the second member. short requires an alignment of 2, so there is 1 byte of padding.
    • 0X00424A38: 2 bytes after the second member. int requires an alignment of 4, but the address is already a multiple of 4. So there is no padding byte.

    Anyway, it is not portable assumption: C standard doesn’t force anything here. It just allow padding bytes between your members and at the end of the structure.

    By the way, you should rather use the following formats:

    • %p and typecast for pointers;
    • %zu or %u with typecast for sizeof.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How does a compiled C++ class look like? Hi all, bash$cat struct.c
Possible Duplicate: Struct Padding The program is as below: #include <iostream> using namespace std;
Possible Duplicate: How do I find the size of a struct? Struct varies in
Possible Duplicate: C implementation of skew heap I have the following struct: typedef struct
Possible Duplicate: Size of struct with a single element Given any type A and
Possible Duplicate: determine size of dynamically allocated memory in c I have a struct
Possible Duplicate: What does 'unsigned temp:3' means A struct 's definition goes like this,
Possible Duplicate: What does 'unsigned temp:3' means struct Test { unsigned a : 5;
Possible Duplicate: Why isn’t sizeof for a struct equal to the sum of sizeof
Possible Duplicate: Why isn't sizeof for a struct equal to the sum of sizeof

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.