Table of Contents
Case 3
Sample code
Output 2
Home Backend Development C++ Why in C/C++, the sizeof of the structure is not equal to the sum of the sizeof of each member?

Why in C/C++, the sizeof of the structure is not equal to the sum of the sizeof of each member?

Aug 26, 2023 am 09:29 AM
Structure c/c++ sizeof

sizeof() The size of the structure type elements obtained is not always equal to the size of each individual member. Sometimes the compiler adds some padding to avoid alignment problems. So dimensions may change. Padding is added when a structure member is followed by a member of larger size or is at the end of the structure. Different compilers have different types of alignment constraints. In the C standard, the total alignment structure is implementation-dependent.

Case 1

In this case, the double z is 8 bytes long, which is larger than x (4 bytes). So another 4 bytes of padding are added. Additionally, the short type data y has 2 bytes of space in memory, so an extra 6 bytes are added as padding.

Why in C/C++, the sizeof of the structure is not equal to the sum of the sizeof of each member?

Sample code

#include <stdio.h>
struct myStruct {
   int x; //Integer takes 4 bytes, and padding 4 bytes
   double z; //Size of double is 8-byte, no padding
   short int y; //Size of short is 2-byte, padding 6-bytes
};
main() {
   printf("Size of struct: %d", sizeof(struct myStruct));
}
Copy after login

Output 2

Size of struct: 24
Copy after login

Case 2

In this case, first insert the double Precision number, which occupies 8 bytes of space. Now the integer x (4 bytes) is added. So there's still another 4 bytes of space. After adding the short y, it can be put into an additional 4 bytes of space, taking up a total of 16 bytes of space.

Why in C/C++, the sizeof of the structure is not equal to the sum of the sizeof of each member?

Sample code

#include <stdio.h>
struct myStruct {
   double z; //Size of double is 8-byte, no padding
   int x; //Integer takes 4 bytes, and padding 4 bytes
   short int y; //Size of short is 2-byte, padding 6-bytes
};
main() {
   printf("Size of struct: %d", sizeof(struct myStruct));
}
Copy after login

Output 2

Size of struct: 16
Copy after login
Copy after login

Case 3

The third case also takes up 16 bytes memory space, but arranged in different ways. Since the first member is a double, it is placed first, and then the short type data is added. Now, when an integer is attempted to be inserted, it can be placed into the remaining 6-byte area. Therefore, there is a padding after the short, but no padding after the integer data.

Why in C/C++, the sizeof of the structure is not equal to the sum of the sizeof of each member?

Sample code

#include <stdio.h>
struct myStruct {
   double z; //Size of double is 8-byte, no padding
   short int y; //Size of short is 2-byte, padding 6-bytes
   int x; //Integer takes 4 bytes, and padding 4 bytes
};
main() {
   printf("Size of struct: %d", sizeof(struct myStruct));
}
Copy after login

Output 2

Size of struct: 16
Copy after login
Copy after login

The above is the detailed content of Why in C/C++, the sizeof of the structure is not equal to the sum of the sizeof of each member?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between Structure and Array in C language? What is the difference between Structure and Array in C language? Aug 30, 2023 pm 09:37 PM

In C, both structures and arrays are used as containers of data types, that is, in both structures and arrays we can store data and perform different operations on them. Based on the internal implementation, here are some basic differences between the two. Sr. Number Key Structure Array 1 Definition A structure can be defined as a data structure that is used as a container and can hold variables of different types. Array, on the other hand, is a data structure used as a container that can hold variables of the same type but does not support multiple data type variables. 2 Memory Allocation Memory allocation structures for input data do not have to be in contiguous memory locations. While in the case of arrays, the input data is stored in contiguous memory allocation, which means that arrays store data in a memory model that allocates contiguous memory blocks (i.e., has

Application and operation methods of structures in PHP Application and operation methods of structures in PHP Jul 16, 2023 pm 11:21 PM

As the PHP language continues to develop and grow, the application and operation methods of structures in PHP are becoming increasingly complete. In addition to common variables and arrays, PHP also provides a more flexible data type, namely structure. A structure is a composite data type composed of multiple data members of different types. It can combine related data to form a more complete and structured data. In PHP, you can simulate the behavior and functionality of structures by using classes and objects. First, let's look at how

Golang structure strong transfer: detailed explanation of implementation principles and techniques Golang structure strong transfer: detailed explanation of implementation principles and techniques Apr 03, 2024 pm 03:09 PM

Struct coercion in Golang is to convert the value of one structure type to another type. This can be achieved through techniques such as assertion force transfer, reflection force transfer, and pointer indirect force transfer. Assertion coercion uses type assertions, reflective coercion uses the reflection mechanism, and pointer indirect coercion avoids value copying. The specific steps are: 1. Assertion force transfer: use typeassertion syntax; 2. Reflection force transfer: use reflect.Type.AssignableTo and reflect.Value.Convert functions; 3. Pointer indirect force transfer: use pointer dereference.

Convert structure to JSON string using json.Marshal function Convert structure to JSON string using json.Marshal function Jul 24, 2023 pm 12:54 PM

Use the json.Marshal function to convert a structure into a JSON string. In the Go language, you can use the json.Marshal function to convert a structure into a JSON string. A structure is a data type composed of multiple fields, and JSON is a commonly used lightweight data exchange format. Converting structures to JSON strings makes it easy to exchange data between different systems. Here is a sample code: packagemainimport(&q

Application of anonymous unions and structures in C language Application of anonymous unions and structures in C language Sep 16, 2023 pm 06:45 PM

Here we take a look at what are anonymous unions and structures in C language. Anonymous unions and structures are unnamed unions and structures. Since they have no name, we cannot create a direct object of it. We use it as a nested structure or union. These are examples of anonymous unions and structures. struct{ datatypevariable; ...};union{ datatypevariable; ...};In this example, we are creating

What are the differences between php and c# What are the differences between php and c# Jun 02, 2023 pm 01:45 PM

The differences between php and c# are: 1. The language type system is different, PHP is dynamic, while C# is static type; 2. The platforms used are different, PHP can be cross-platform, while C# is exclusive to Windows; 3. The programming paradigm is different, PHP It supports object-oriented, procedural and functional programming, and C# is more inclined to object-oriented programming; 4. The execution speed is different, PHP is faster, and C# is relatively slow; 5. The application scenarios are different, PHP is used in web development, servers, etc. C# is used for Windows desktop and web applications.

How does golang return a structure? How does golang return a structure? Apr 23, 2024 pm 02:03 PM

How to return struct in Golang? Specify the structure type in the function signature, such as: funcgetPerson()Person{}. Use the return{} statement in the function body to return a structure containing the required fields. Struct fields can be base types or other structures.

Use the json.Unmarshal function to parse a JSON string into a structure Use the json.Unmarshal function to parse a JSON string into a structure Jul 25, 2023 pm 10:49 PM

Use the json.Unmarshal function to parse a JSON string into a structure. In the Go language, you can use the json.Unmarshal function to parse a JSON string into a structure. This is a very useful feature, especially when processing API responses or reading configuration files. First, we need to define a structure type to represent the structure of the JSON object we want to parse. Suppose we have the following JSON string: {"name"

See all articles