C++结构体所占用字节数的疑惑

OrdinaryCoder 码农  2019-07-03 10:54:09

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

#define USER_INFO_LENGTH (512) //用户信息数据长度;
#define INT_FACE_REC_MAX_PERSON_CNT (8)

typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;

typedef signed char int_least8_t;
typedef short int_least16_t;
typedef int int_least32_t;
typedef long long int_least64_t;
typedef unsigned char uint_least8_t;
typedef unsigned short uint_least16_t;
typedef unsigned int uint_least32_t;
typedef unsigned long long uint_least64_t;

typedef struct _st_face_expression
{
uint8_t person_age;
uint8_t person_gender; //gender:0-female 1-male
uint8_t person_smile; //smile
uint8_t person_glasses; //glasses: 0-without glasses 1-with glasses
uint8_t person_beauty; //face score: bigger than 0
uint8_t person_race; //race:
} st_face_expression;

typedef struct _st_face_rec_person_info
{
uint32_t user_id;
uint32_t info_len;
uint8_t info_data[USER_INFO_LENGTH];
} st_face_rec_person_info;

typedef struct _st_face_rec_result_inner
{
uint8_t person_count; //number of person
uint8_t result_code[INT_FACE_REC_MAX_PERSON_CNT]; //status code of recognition result
st_face_rec_person_info person_info[INT_FACE_REC_MAX_PERSON_CNT]; //person information
st_face_expression person_expression[INT_FACE_REC_MAX_PERSON_CNT]; //expression information
uint8_t robotId;
char picPath[1024];
} st_face_rec_result_inner;

int main()
{
st_face_rec_result_inner face;
printf("count占:%d字节\r\n,res占:%d字节\r\n,info占:%d字节\r\n,expression占:%d字节\r\n, id占:%d字节\r\n,pic占:%d字节\r\n", sizeof(face.person_count), sizeof(face.result_code), sizeof(face.person_info), sizeof(face.person_expression), sizeof(face.robotId), sizeof(face.picPath));
printf("总字节数:%d\r\n", sizeof(face.person_count) + sizeof(face.result_code) + sizeof(face.person_info) + sizeof(face.person_expression) + sizeof(face.robotId) + sizeof(face.picPath));
printf("st_face_rec_result_inner字节数:%d", sizeof(st_face_rec_result_inner));
system("pause");
return 0;
}
为什么用sizeof计算st_face_rec_result_inner结构所占内存就是5248,而把组成这个结构各部分都用sizeof计算一下所占内存再求和就是5242 差了6个字节

...全文
147 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
636f6c696e 2019-07-03
https://www.cnblogs.com/clover-toeic/p/3853132.html 一般系统默认都是4字节对齐。 实际空间结构如下:
typedef struct _st_face_rec_result_inner
{
    uint8_t person_count;                                                  	1
    uint8_t result_code[INT_FACE_REC_MAX_PERSON_CNT];                      	8
	// 预留3字节															3
    st_face_rec_person_info person_info[INT_FACE_REC_MAX_PERSON_CNT];      	520*8=4160	
    st_face_expression person_expression[INT_FACE_REC_MAX_PERSON_CNT]; 		6*8=48
    uint8_t robotId;														1
    char picPath[1024];														1024
	// 预留3字节															3
} st_face_rec_result_inner;
回复
636f6c696e 2019-07-03
额,这就是结构体字节对齐啊,C语言的基础知识啊
回复
ggglivw 2019-07-03
baidu字节对齐了解下
回复
liups 2019-07-03
引用 3 楼 OrdinaryCoder 的回复:
[quote=引用 2 楼 轻箬笠 的回复:] 结构体都有对齐的可能的。如果要一致的话 #pragma pack(push,1) 把你的结构体放到这里 #pragma pack(pop)
已经改了[/quote] 通常不要改!这关系到效率问题
回复
OrdinaryCoder 2019-07-03
引用 2 楼 轻箬笠 的回复:
结构体都有对齐的可能的。如果要一致的话

#pragma pack(push,1)
把你的结构体放到这里
#pragma pack(pop)

已经改了
回复
轻箬笠 2019-07-03
结构体都有对齐的可能的。如果要一致的话

#pragma pack(push,1)
把你的结构体放到这里
#pragma pack(pop)
回复
OrdinaryCoder 2019-07-03
看数值是差了6个字节 感觉应该出在 uint8_t person_count; 字段和uint8_t robotId;这个字段上 应该是系统默认把这俩字段补齐成int了
回复
赵4老师 2019-07-03
#include <stdio.h>
#define field_offset(s,f) (int)(&(((struct s *)(0))->f))
struct AD  { int a; char b[13]; double c;};
#pragma pack(push)
#pragma pack(1)
struct A1  { int a; char b[13]; double c;};
#pragma pack(2)
struct A2  { int a; char b[13]; double c;};
#pragma pack(4)
struct A4  { int a; char b[13]; double c;};
#pragma pack(8)
struct A8  { int a; char b[13]; double c;};
#pragma pack(16)
struct A16 { int a; char b[13]; double c;};
#pragma pack(pop)
int main() {
    printf("AD.a %d\n",field_offset(AD,a));
    printf("AD.b %d\n",field_offset(AD,b));
    printf("AD.c %d\n",field_offset(AD,c));
    printf("AD sizeof %d\n", sizeof(AD));
    printf("\n");
    printf("A1.a %d\n",field_offset(A1,a));
    printf("A1.b %d\n",field_offset(A1,b));
    printf("A1.c %d\n",field_offset(A1,c));
    printf("A1 sizeof %d\n", sizeof(A1));
    printf("\n");
    printf("A2.a %d\n",field_offset(A2,a));
    printf("A2.b %d\n",field_offset(A2,b));
    printf("A2.c %d\n",field_offset(A2,c));
    printf("A2 sizeof %d\n", sizeof(A2));
    printf("\n");
    printf("A4.a %d\n",field_offset(A4,a));
    printf("A4.b %d\n",field_offset(A4,b));
    printf("A4.c %d\n",field_offset(A4,c));
    printf("A4 sizeof %d\n", sizeof(A4));
    printf("\n");
    printf("A8.a %d\n",field_offset(A8,a));
    printf("A8.b %d\n",field_offset(A8,b));
    printf("A8.c %d\n",field_offset(A8,c));
    printf("A8 sizeof %d\n", sizeof(A8));
    printf("\n");
    printf("A16.a %d\n",field_offset(A16,a));
    printf("A16.b %d\n",field_offset(A16,b));
    printf("A16.c %d\n",field_offset(A16,c));
    printf("A16 sizeof %d\n", sizeof(A16));
    printf("\n");
    return 0;
}
//AD.a 0
//AD.b 4
//AD.c 24
//AD sizeof 32
//
//A1.a 0
//A1.b 4
//A1.c 17
//A1 sizeof 25
//
//A2.a 0
//A2.b 4
//A2.c 18
//A2 sizeof 26
//
//A4.a 0
//A4.b 4
//A4.c 20
//A4 sizeof 28
//
//A8.a 0
//A8.b 4
//A8.c 24
//A8 sizeof 32
//
//A16.a 0
//A16.b 4
//A16.c 24
//A16 sizeof 32
//
//
回复
相关推荐
发帖
C++ 语言
创建于2007-09-28

6.0w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2019-07-03 10:54
社区公告
暂无公告