Addition of structure C

I can use the pragma package for various compilers to force structures to have fields that are not on their natural alignments.

Is this recursive - so suppose that struct typedef A contains a typedef field of type B. If pragma is used to pack A, will this force B be packed?

+3
source share
4 answers

Just don't do it. The exact behavior of an ugly custom extension is not something you can even ask questions about without specifying the exact platform / compiler you're working with. If you need a packaged structure, you better understand why your code is broken and fix it. Packed structures are a bandage for broken code that does nothing to eliminate the root cause of the violation.

+5
source

You should hope not! Suppose you have a function that takes a struct A parameter:

void fn( struct A x ) ;

and then a packed structure B, in which there is a structure A as a member:

#pragma pack(1)
struct B
{
    struct A a ; 
}

If you pass member a from B to fn (), the function does not know about the packaging in this instance and will not work.

, , V++ 2010:

struct A
{
    int a;
    char b;
    int c ;
} ;

struct B
{
    struct A  d ;
} ;

#pragma pack(1)
struct C
{
    struct A  d ;
} ;


#pragma pack(1)
struct D
{
    int a;
    char b;
    int c ;
} ;

#pragma pack(1)
struct E
{
    struct D ;
} ;

int main()
{
    int a = sizeof(struct A) ;
    int b = sizeof(struct B) ;
    int c = sizeof(struct C) ;
    int d = sizeof(struct D) ;
    int e = sizeof(struct E) ;
}

a, b, c, d e main() :

  • a = 12
  • b = 12
  • c = 12
  • d = 9
  • e = 9

struct C A.

+5

Nathon, :

#include <stdio.h>

#if defined PACK_FIRST || defined PACK_BOTH
#pragma pack(1)
#endif
struct inner {
  char a;
  double b;
};

#if defined PACK_SECOND || defined PACK_BOTH
#pragma pack(1)
#endif
struct outer {
  char a;
  struct inner b;
  char c;
  double d;
};

int main(void) {
  printf("sizeof (char): %d (1, of course)\n", (int)sizeof (char));
  printf("sizeof (double): %d\n", (int)sizeof (double));
  printf("sizeof (inner): %d\n", (int)sizeof (struct inner));
  printf("sizeof (outer): %d\n", (int)sizeof (struct outer));
  return 0;
}
$ gcc 4128061.c
$ ./a.out 
sizeof (char): 1 (1, of course)
sizeof (double): 8
sizeof (inner): 16
sizeof (outer): 40
$ gcc -DPACK_FIRST 4128061.c
$ ./a.out 
sizeof (char): 1 (1, of course)
sizeof (double): 8
sizeof (inner): 9
sizeof (outer): 19
$ gcc -DPACK_SECOND 4128061.c
$ ./a.out 
sizeof (char): 1 (1, of course)
sizeof (double): 8
sizeof (inner): 16
sizeof (outer): 26
$ gcc -DPACK_BOTH 4128061.c
$ ./a.out 
sizeof (char): 1 (1, of course)
sizeof (double): 8
sizeof (inner): 9
sizeof (outer): 19

-, gcc- , #pragma.

+4

GCC , :

#include <stdio.h>
typedef struct
{
    char a;
    int  b;
}inner_t;
#pragma pack(1)
typedef struct
{
    char a;
    inner_t inner;
} outer_t;

int main()
{
    outer_t outer;

    outer.inner.a = 'a';
    outer.inner.b = 0xABCDEF01;
    printf ("outer.inner.a: %c\n", outer.inner.a);
    return 0;
}

gdb, printf, ...

(gdb) x/5xw &outer
0x7fffffffe4b0: 0xffff61a0      0xcdef01ff      0x000000ab      0x00000000
0x7fffffffe4c0: 0x00000000

inner.bnot aligned by word. Thus, in GCC 4.4.5, on a small, finite 64-bit machine, nested structures are packed if the external structure is packed. Forgive my typedefs, those of you who they don't like.

+1
source

Source: https://habr.com/ru/post/1773703/


All Articles