ssinfod
ssinfod

Reputation: 1051

#pragma pack(show) with GCC

Is there a way to show the memory "pack" size with GCC ?

In Microsoft Visual C++, I am using:

 #pragma pack(show) 

which displays the value in a warning message; see Microsoft's documentation.

What is the equivalent with GCC?

Upvotes: 1

Views: 1932

Answers (2)

Morten Jensen
Morten Jensen

Reputation: 5936

I use a static assertion whenever I pack a structure and want to see its size.

/*
   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);

This will give you a compile time warning whenever the static assertion fails.

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

Since I can't see such functionality listed in the pertinent documentation, I'm going to conclude that GCC cannot do this.

Upvotes: 2

Related Questions