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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:23:23+00:00 2026-06-13T11:23:23+00:00

Possible Duplicate: Struct Padding The program is as below: #include <iostream> using namespace std;

  • 0

Possible Duplicate:
Struct Padding

The program is as below:

#include <iostream>

using namespace std;

struct node1 {
    int id;
    char name[4];
};

struct node2 {
    int id;
    char name[3];
};

int
main(int argc, char* argv[])
{
    cout << sizeof(struct node1) << endl;
    cout << sizeof(struct node2) << endl;
    return 0;
}

And the compiler is g++ (GCC) 4.6.3. The outputs are:

8
8

I really do not understand why is it so. Why is the output of sizeof(struct node2) not 7?

  • 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-13T11:23:24+00:00Added an answer on June 13, 2026 at 11:23 am

    This is because structures are aligned at boundaries. Usually of 4 bytes(although it can be changed) – Which means, each element in the structure is atleast 4 bytes and if the size of any element is less than 4 bytes, then padding is added to them at the end.

    hence both are 8 bytes.

    size of int = 4
    size of char = 1 
    size of char array of 3 elements = 3
    
    total size = 7, padding added (because of boundary) = +1 byte
    
    for second structure:
    
    sizeof int = 4
    sizeof char = 1
    sizeof char array of 4 elements = 4
    
    total size = 8. no padding required. 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What are the differences between struct and class in C++ http://www.cplusplus.com/reference/std/typeinfo/type_info/ I
Possible Duplicate: Copying one structure to another struct node { int n; struct classifier
Possible Duplicate: Set all bytes of int to (unsigned char)0, guaranteed to represent zero?
Possible Duplicate: What is the difference between using a struct with two fields and
Possible Duplicate: How to prevent crash in C++ typedef struct { int val; }
Possible Duplicate: What means the dot before variable name in struct? const struct file_operations
Possible Duplicate: What does 'unsigned temp:3' means struct Test { unsigned a : 5;
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: C++ struct sorting Is it possible to sort a vector in C++
Possible Duplicate: Circular definition in C typedef struct{ node *next; node *last; } doubleLink;

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.