RicardasSim

sizeof struct

Sep 9th, 2022
1,463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. // gcc, 64bit, -std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. struct s1 {
  7.    
  8.     int a;
  9.     int b;
  10.     int c;
  11.     int *p;
  12.  
  13. };
  14.  
  15. typedef struct s2 {
  16.    
  17.     int a;
  18.     int b;
  19.     int c;
  20.     int *p;
  21.  
  22. } S2;
  23.  
  24. typedef struct s3 {
  25.    
  26.     int a;
  27.     int b __attribute__ ((packed));
  28.     int c __attribute__ ((packed));
  29.     int *p __attribute__ ((packed));
  30.  
  31. } S3;
  32.  
  33. typedef struct s4 {
  34.    
  35.     int a;
  36.     int b __attribute__ ((aligned (16)));
  37.     int c __attribute__ ((aligned (16)));
  38.     int *p __attribute__ ((aligned (16)));
  39.  
  40. } S4;
  41.  
  42. struct s1 strc_1;
  43.  
  44. struct s1 strc_1a[10];
  45.  
  46. S2 strc_2;
  47.  
  48. S2 strc_2a[10];
  49.  
  50. S3 strc_3;
  51.  
  52. S3 strc_3a[10];
  53.  
  54. S4 strc_4;
  55.  
  56. S4 strc_4a[10];
  57.  
  58. int main() {
  59.  
  60.     printf("sizeof struct s1: %zu\n", sizeof (struct s1));
  61.    
  62.     printf("sizeof strc_1: %zu\n", sizeof strc_1);
  63.    
  64.     printf("sizeof strc_1a: %zu\n", sizeof strc_1a);
  65.    
  66.     printf("sizeof *strc_1a: %zu\n", sizeof *strc_1a);
  67.    
  68.     printf("sizeof strc_1a[0]: %zu\n\n", sizeof strc_1a[0]);
  69.  
  70.    
  71.     printf("sizeof (S2): %zu\n", sizeof (S2));
  72.    
  73.     printf("sizeof strc_2: %zu\n", sizeof strc_2);
  74.    
  75.     printf("sizeof strc_2a: %zu\n", sizeof strc_2a);
  76.  
  77.     printf("sizeof *strc_2a: %zu\n", sizeof *strc_2a);
  78.    
  79.     printf("sizeof strc_2a[0]: %zu\n\n", sizeof strc_2a[0]);
  80.  
  81.    
  82.     printf("sizeof (S3): %zu\n", sizeof (S3));
  83.    
  84.     printf("sizeof strc_3: %zu\n", sizeof strc_3);
  85.    
  86.     printf("sizeof strc_3a: %zu\n", sizeof strc_3a);
  87.  
  88.     printf("sizeof *strc_3a: %zu\n", sizeof *strc_3a);
  89.    
  90.     printf("sizeof strc_3a[0]: %zu\n\n", sizeof strc_3a[0]);
  91.  
  92.  
  93.    
  94.     printf("sizeof (S4): %zu\n", sizeof (S4));
  95.    
  96.     printf("sizeof strc_4: %zu\n", sizeof strc_4);
  97.    
  98.     printf("sizeof strc_4a: %zu\n", sizeof strc_4a);
  99.  
  100.     printf("sizeof *strc_4a: %zu\n", sizeof *strc_4a);
  101.    
  102.     printf("sizeof strc_4a[0]: %zu\n\n", sizeof strc_4a[0]);
  103.    
  104.     return 0;
  105. }
  106.  
  107. /*
  108. output:
  109.  
  110. sizeof struct s1: 24
  111. sizeof strc_1: 24
  112. sizeof strc_1a: 240
  113. sizeof *strc_1a: 24
  114. sizeof strc_1a[0]: 24
  115.  
  116. sizeof (S2): 24
  117. sizeof strc_2: 24
  118. sizeof strc_2a: 240
  119. sizeof *strc_2a: 24
  120. sizeof strc_2a[0]: 24
  121.  
  122. sizeof (S3): 20
  123. sizeof strc_3: 20
  124. sizeof strc_3a: 200
  125. sizeof *strc_3a: 20
  126. sizeof strc_3a[0]: 20
  127.  
  128. sizeof (S4): 64
  129. sizeof strc_4: 64
  130. sizeof strc_4a: 640
  131. sizeof *strc_4a: 64
  132. sizeof strc_4a[0]: 64
  133.  
  134. */
  135.  
Advertisement
Add Comment
Please, Sign In to add comment