2014-04-29 4 views
0

GCC에 메모리 "팩"크기를 표시하는 방법이 있습니까?GCC로 #pragma pack (show)

의 Microsoft Visual C에서

++, 내가 사용하고 있습니다 :

경고 메시지의 값을 표시
#pragma pack(show) 

; Microsoft's documentation을 참조하십시오.

GCC와 동일한 기능은 무엇입니까?

+3

[documentation] (http://gcc.gnu.org/onlinedocs/gcc/Structure-Packing-Pragmas.html)에이 파일이 하나도 없습니다. –

+1

@Elias : [tag : c] 태그에 어떤 문제가 있습니까? –

+0

@LightnessRacesinOrbit : OP가 VC++에서 제공되었고 C++가이 질문에 태그를 추가했음을 감안할 때, 그가 쓰는 코드가 C가 아닌 C++로 작성된 것 같습니다. 전처리 기가 동일해야하지만 항상 그래야하는 것은 아닙니다. case –

답변

2

the pertinentdocumentation에 나열된 기능을 볼 수 없으므로 GCC가이 작업을 수행 할 수 없다고 결론을 내릴 것입니다.

+0

이 작업을 수행 할 수는 없지만 상황에 따라 C++ 11 offsetof가 도움이 될 수도 있습니다. http://en.cppreference.com/w/cpp/types/offsetof 예를 들어 단위 테스트. (나는 결코 그것을 사용하지 않았기 때문에 포장으로 예상대로 작동하는지 잘 모르겠습니다.) – meandbug

+0

@meandbug : 예, 가능합니다. –

0

구조체를 압축하고 그 크기를보고 싶을 때마다 정적 어설 션을 사용합니다. 정적 어설 션이 실패 할 때마다

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

이렇게하면 컴파일 시간 경고를 줄 것이다.