Skip to content
Related Articles

Related Articles

Improve Article

Why does empty Structure has size 1 byte in C++ but 0 byte in C

  • Last Updated : 04 Aug, 2021

A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. The ‘struct’ keyword is used to create a structure. The general syntax for creating a structure is as shown below:

Syntax-

struct structureName{
    member1;
    member2;
    member3;
    .
    .
    .
    memberN;
};

Structures in C++ can contain two types of members: 

  1. Data Member These members are normal C++ variables. A structure can be created with variables of different data types in C++.
  2. Member Functions These members are normal C++ functions. Along with variables, the functions can also be included inside a structure declaration.

Problem Statement: Why the size of an empty structure is not zero in C++ but zero in C.

Solution:
Below is the C program with an empty structure:



C




// C program with an empty
// structure
#include <stdio.h>
 
// Driver code
int main()
{
    // Empty Structure
    struct empty {
    };
 
    // Initializing the Variable
    // of Struct type
    struct empty empty_struct;
 
    // Printing the Size of Struct
    printf("Size of Empty Struct in C programming = %ld",
           sizeof(empty_struct));
}


Output

Size of Empty Struct in C programming = 0

Below is the C++ program with an empty structure:

C++




// C++ program to implement
// an empty structure
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    // Empty Struct
    struct empty {
    };
 
    // Initializing the Variable
    // of Struct type
    struct empty empty_struct;
 
    // Printing the Size of Struct
    cout << "Size of Empty Struct in C++ Programming = " << sizeof(empty_struct);
}


Output

Size of Empty Struct in C++ Programming = 1

If observed carefully, the same code is executed in C and C++, but the output is different in both cases. Let’s discuss the reason behind this-

  1. The C++ standard does not permit objects (or classes) of size 0. This is because that would make it possible for two distinct objects to have the same memory location. This is the reason behind the concept that even an empty class and structure must have a size of at least 1. It is known that the size of an empty class is not zero. Generally, it is 1 byte. The C++ Structures also follow the same principle as the C++ Classes follow, i.e. that structures in c++ will also not be of zero bytes. The minimum size must be one byte.
  2. Creating an empty structure in C/C++ is a syntactic constraint violation. However, GCC permits an empty structure in C as an extension. Furthermore, the behavior is undefined if the structure does not have any named members because:

C99 says- 
If the struct-declaration-list contains no named members, the behavior is undefined.

This implies- 

// Constraint Violation
struct EmptyGeeksforGeeks {};

// Behavior undefined, 
// since there is no named member
struct EmptyGeeksforGeeks {int :0 ;}; 

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :