From 091a595c8f44bab27a31ff43a8a2f93204404e7d Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Fri, 13 Mar 2020 19:27:10 +0800 Subject: [PATCH 1/2] Size fo empty structs in C is zero byte. --- src/type-layout.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 51ba859ef..003a3ce63 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -236,8 +236,9 @@ the sake of clarity. To perform memory layout computations in actual code, use -> Note: This algorithm can produce zero-sized structs. This differs from -> C where structs without data still have a size of one byte. +> Note: This algorithm can produce zero-sized structs. In C, the size of +> structs without data is zero. This is not the same as C++ where structs +> without data still have a size of one byte. #### \#[repr(C)] Unions From 30af09147d38d63aed24a7d707df9aeb5524d5b2 Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Fri, 13 Mar 2020 21:55:21 +0800 Subject: [PATCH 2/2] Further improvement --- src/type-layout.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/type-layout.md b/src/type-layout.md index 003a3ce63..7557d951c 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -236,9 +236,12 @@ the sake of clarity. To perform memory layout computations in actual code, use -> Note: This algorithm can produce zero-sized structs. In C, the size of -> structs without data is zero. This is not the same as C++ where structs -> without data still have a size of one byte. +> Note: This algorithm can produce zero-sized structs. In C, an empty struct +> declaration like `struct Foo { }` is illegal. However, both gcc and clang +> support options to enable such structs, and assign them size zero. C++, in +> contrast, gives empty structs a size of 1, unless they are inherited from or +> they are fields that have the `[[no_unique_address]]` attribute, in which +> case they do not increase the overall size of the struct. #### \#[repr(C)] Unions