64,651
社区成员
发帖
与我相关
我的任务
分享
#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个字节
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;
#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
//
//