RicardasSim

struct packed aligned

Dec 26th, 2018
725
0
Never
8
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main ()
  5. {
  6.  
  7.     struct st1{
  8.         char a;
  9.         char* a1;
  10.         char* a2;
  11.     };
  12.  
  13.     struct st2{
  14.         char a;
  15.         char* a1 __attribute__ ((packed));
  16.         char* a2 __attribute__ ((packed));
  17.     };
  18.  
  19.     struct st3{
  20.         int a;
  21.         char* a1;
  22.         char* a2;
  23.     };
  24.  
  25.     struct st4{
  26.         int a;
  27.         char* a1 __attribute__ ((packed));
  28.         char* a2 __attribute__ ((packed));
  29.     };
  30.  
  31.     struct st5{
  32.         char a __attribute__ ((aligned (16)));
  33.         char* a1 __attribute__ ((aligned (16)));
  34.         char* a2 __attribute__ ((aligned (16)));
  35.     };
  36.  
  37.     struct st6{
  38.         char a;
  39.         char* a1;
  40.         char* a2;
  41.     } __attribute__ ((aligned (32)));
  42.    
  43.    
  44.     printf("sizeof int: %zu\n", sizeof(int));
  45.     printf("sizeof char*: %zu\n", sizeof(char*));
  46.    
  47.     printf("(1) %zu\n", sizeof (struct st1));
  48.     printf("(2) %zu\n", sizeof (struct st2));
  49.     printf("(3) %zu\n", sizeof (struct st3));
  50.     printf("(4) %zu\n", sizeof (struct st4));
  51.     printf("(5) %zu\n", sizeof (struct st5));
  52.     printf("(6) %zu\n\n", sizeof (struct st6));
  53.  
  54.    
  55. }
  56.  
  57. /*
  58. output:
  59.  
  60. sizeof int: 4
  61. sizeof char*: 8
  62. (1) 24
  63. (2) 17
  64. (3) 24
  65. (4) 20
  66. (5) 48
  67. (6) 32
  68.  
  69. */
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment