我正在使用32位机器,所以我认为内存对齐应该是4个字节.说我有结构:
typedef struct {
unsigned short v1;
unsigned short v2;
unsigned short v3;
} myStruct;
Run Code Online (Sandbox Code Playgroud)
实际大小是6个字节,我想对齐大小应该是8,但sizeof(myStruct)
返回6.
但是,如果我写:
typedef struct {
unsigned short v1;
unsigned short v2;
unsigned short v3;
int i;
} myStruct;
Run Code Online (Sandbox Code Playgroud)
实际大小是10个字节,对齐是12,这次sizeof(myStruct) == 12
.
有人可以解释一下有什么区别吗?
考虑以下C代码:
#include <stdio.h>
struct employee
{
int id;
char name[30];
};
int main()
{
struct employee e1;
printf("%d %d %d", sizeof(e1.id), sizeof(e1.name), sizeof(e1));
return(0);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
4 30 36
为什么结构的大小不等于其各个组件变量的大小总和?
当我只运行代码片段时
int *t;
std::cout << sizeof(char) << std::endl;
std::cout << sizeof(double) << std::endl;
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(t) << std::endl;
Run Code Online (Sandbox Code Playgroud)
它给我一个这样的结果:
1
8
4
4
Run Code Online (Sandbox Code Playgroud)
总计:17.
但是,当我测试包含这些数据类型的sizeof结构时,它给了我24,我很困惑.额外的7个字节是多少?
这是代码
#include <iostream>
#include <stdio.h>
struct struct_type{
int i;
char ch;
int *p;
double d;
} s;
int main(){
int *t;
//std::cout << sizeof(char) <<std::endl;
//std::cout << sizeof(double) <<std::endl;
//std::cout << sizeof(int) <<std::endl;
//std::cout << sizeof(t) <<std::endl;
printf("s_type is %d byes long",sizeof(struct struct_type));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
:编辑
我已经像这样更新了我的代码
#include <iostream> …
Run Code Online (Sandbox Code Playgroud) 在各种3d数学代码库中,我有时会遇到这样的事情:
struct vec {
float x, y, z;
float& operator[](std::size_t i)
{
assert(i < 3);
return (&x)[i];
}
};
Run Code Online (Sandbox Code Playgroud)
其中,AFAIK是非法的,因为允许实现在成员之间虚假地添加填充,即使它们属于同一类型,但实际上没有人会这样做.
通过static_assert
s 施加约束可以使这变得合法吗?
static_assert(sizeof(vec) == sizeof(float) * 3);
Run Code Online (Sandbox Code Playgroud)
即static_assert
没有被触发意味着operator[]
什么是预期的并且不会在运行时调用UB?
c++ strict-aliasing language-lawyer type-punning structure-packing
具有位域的结构,即使在“打包”时,似乎也会根据指定的 int 类型来处理位域的大小(以及对齐方式?)。有人可以指出定义该行为的 C++ 规则吗?我尝试了十几个编译器和体系结构(谢谢你,编译器资源管理器!),结果在所有编译器和体系结构中都是一致的。
这是要使用的代码: https: //godbolt.org/z/31zMcnboY
#include <cstdint>
#pragma pack(push, 1)
struct S1{ uint8_t v: 1; }; // sizeof == 1
struct S2{ uint16_t v: 1; }; // sizeof == 2
struct S3{ uint32_t v: 1; }; // sizeof == 4
struct S4{ unsigned v: 1; }; // sizeof == 4
#pragma pack(pop)
auto f(auto s){ return sizeof(s); }
int main(){
f(S1{});
f(S2{});
f(S3{});
f(S4{});
}
Run Code Online (Sandbox Code Playgroud)
生成的 ASM 清楚地显示了、
的返回大小分别f()
为 1、2、4 :S1
S2 …
c++ bit-packing language-lawyer bit-fields structure-packing
我试图理解位域的概念.但我无法找到CASE III中以下结构的大小为8字节的原因.
struct B
{
unsigned char c; // +8 bits
} b;
Run Code Online (Sandbox Code Playgroud)
的sizeof(B); //输出:1(因为unsigned char在我的系统上占用1个字节)
struct B
{
unsigned b: 1;
} b;
sizeof(b); // Output: 4 (because unsigned takes 4 bytes on my system)
Run Code Online (Sandbox Code Playgroud)
struct B
{
unsigned char c; // +8 bits
unsigned b: 1; // +1 bit
} b;
sizeof(b); // Output: 8
Run Code Online (Sandbox Code Playgroud)
我不明白为什么案例III的输出为8.我期待1(char)+ 4(无符号)= 5.
为什么sizeof();
这个结构有16个字节?我正在用g ++编译.
struct bitmapfileheader {
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
};
Run Code Online (Sandbox Code Playgroud) 可能的重复:
为什么结构的 sizeof 不等于每个成员的 sizeof 之和?
将结构的成员声明为 uint32_t 时的额外字节
出于某种原因,sizeof
运算符返回此结构的虚假大小(48
而不是40
):
typedef struct mbdb_file_info {
uint16_t mode;
uint32_t unk0;
uint32_t unk1;
uint32_t user_id;
uint32_t group_id;
uint32_t time0;
uint32_t time1;
uint32_t time2;
uint64_t length;
uint8_t flag;
uint8_t property_count;
} mbdb_file_info;
Run Code Online (Sandbox Code Playgroud)
所以这是一个简单的测试:
printf("%ld %ld %ld %ld: %ld", sizeof(uint8_t),
sizeof(uint16_t),
sizeof(uint32_t),
sizeof(uint64_t),
sizeof(mbdb_file_info));
Run Code Online (Sandbox Code Playgroud)
哪个打印:
1 2 4 8: 48
这是怎么发生的?如果你把所有的尺寸加在一起,你会得到40
,而不是48
。到底是48
从哪里来的?
如果这是一些奇怪的x86-64
特权,我如何确保结构的所有字段占用我希望它们占用的数量(我正在向这个结构投射一堆字节)?