首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >#show(Show)和GCC

#show(Show)和GCC
EN

Stack Overflow用户
提问于 2014-04-29 22:36:36
回答 2查看 1.4K关注 0票数 1

有没有办法和GCC一起展示内存“包”的大小?

在Microsoft Visual C++中,我使用:

代码语言:javascript
运行
复制
 #pragma pack(show) 

它在警告消息中显示值;请参见Microsoft's documentation

与GCC相当的是什么?

EN

回答 2

Stack Overflow用户

发布于 2014-04-29 22:46:12

因为我在the pertinent documentation中看不到这样的功能,所以我会得出结论,GCC不能做到这一点。

票数 2
EN

Stack Overflow用户

发布于 2014-04-29 23:00:23

每当我打包一个结构并想要查看它的大小时,我都会使用静态断言。

代码语言:javascript
运行
复制
/*
   The static_assert macro will generate an error at compile-time, if the predicate is false
   but will only work for predicates that are resolvable at compile-time!

   E.g.: to assert the size of a data structure, static_assert(sizeof(struct_t) == 10)
*/
#define STATIC_ASSERT(COND,MSG)      typedef char static_assertion_##MSG[(!!(COND))*2-1]
/* token pasting madness: */
#define COMPILE_TIME_ASSERT3(X,L)     STATIC_ASSERT(X,at_line_##L)             /* add line-number to error message for better warnings, especially GCC will tell the name of the variable as well */
#define COMPILE_TIME_ASSERT2(X,L)     COMPILE_TIME_ASSERT3(X, L)               /* expand line-number */
#define static_assert(X)              COMPILE_TIME_ASSERT2(X, __LINE__)        /* call with line-number macro */

#define PACKED  __attribute__ ((gcc_struct, __packed__))

typedef struct {
  uint8_t bytes[3];
  uint32_t looong;
} PACKED struct_t;
static_assert(sizeof(struct_t) == 7);

每当静态断言失败时,这将给您一个编译时警告。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23367775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档