Is there a way to find the packed size of a structure defined and declared without packed attribute in GCC compiler?
Example:
struct Name
{
int a;
char ch;
}
any function or macro like get_packed_size(Name)
should return 5
Define your struct using a macro that provides the required information. For example (though there are other possible implementations):
#define DEFINE_STRUCT_WITH_KNOWN_PACKED_SIZE(StructName, StructBody)\
struct StructName StructBody\
struct __attribute__ ((__packed__)) StructName##_packed StructBody
#define GET_PACKED_SIZE(StructName) sizeof (struct StructName##_packed)
DEFINE_STRUCT_WITH_KNOWN_PACKED_SIZE(Name, {
int a;
char ch;
};)
#include <stdio.h>
int main() {
printf("%lu", GET_PACKED_SIZE(Name));
}
不,没有办法......你只有sizeof()
,你需要照顾填充......
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.