Skip to content

Fix 1: wrong statement in no_unique_address section#38

Merged
Charliechen114514 merged 1 commit into
Awesome-Embedded-Learning-Studio:mainfrom
YukunJ:fix-typo-1
Jun 10, 2026
Merged

Fix 1: wrong statement in no_unique_address section#38
Charliechen114514 merged 1 commit into
Awesome-Embedded-Learning-Studio:mainfrom
YukunJ:fix-typo-1

Conversation

@YukunJ

@YukunJ YukunJ commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Brief: Fix wrong statement about the struct size in no_unique_address section

In the vast majority of compiler implementation, the struct Container will have the padding and be of 8 bytes in size due to the memory address alignment requirement.

Please refer to: https://godbolt.org/z/Gfo7o615q

@Charliechen114514

@Charliechen114514

Copy link
Copy Markdown
Member

非常感谢您的反馈,我这边本地也顺手验证(作为典型的本地机器)

验证环境

  • 编译器:GCC (Ubuntu), -std=c++20
  • 平台:x86-64 (WSL2 Linux 6.18)

编译与运行验证

使用 PR 中涉及的三个结构体编写了测试程序,编译运行结果如下:

类型 sizeof 说明
Empty 1 ✅ 符合预期
Container 8 ✅ PR 修正后 static_assert(sizeof(Container) == 8) 通过
PackedContainer 5 static_assert(sizeof(PackedContainer) == sizeof(int) + 1) 通过
NoUniqueAddrContainer 4 EBO 生效,Empty 不占空间

原文的确存在问题

原文的断言:

static_assert(sizeof(Container) == sizeof(int) + 1);  // 可能有 padding

实际 sizeof(Container) == 8,而 sizeof(int) + 1 == 5编译失败。原文断言是错误的。

这个修改没有显著问题,非常感谢您的批评指正!

@Charliechen114514 Charliechen114514 merged commit 105754f into Awesome-Embedded-Learning-Studio:main Jun 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants