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

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: 
Data Member– These members are normal C++ variables. A structure can be created with variables of different data types in C++.Member Functions– These members are normal C++ functions. Along with variables, the functions can also be included inside a structure declaration.
Data Member– These members are normal C++ variables. A structure can be created with variables of different data types in C++.
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 codeint 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));}
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 codeint 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);}
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-
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.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:
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.
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 ;}; 
 
surindertarika1234
C-Structure & Union
cpp-structure
memory-management
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Unordered Sets in C++ Standard Template Library
What is the purpose of a function prototype?
Operators in C / C++
Exception Handling in C++
Smart Pointers in C++ and How to Use Them
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++