RicardasSim icon

sizeof struct

RicardasSim | PRO | 09/09/22 11:07:56 AM UTC | 0 ⭐ | 1423 👁️ | Never ⏰ | []
C |

2.39 KB

|

None

|

0 👍

/

0 👎

// gcc, 64bit, -std=c99 -Wall -Wextra -Wpedantic -Wshadow
 
#include <stdio.h>
#include <stdlib.h>
 
struct s1 {
    
    int a;
    int b;
    int c;
    int *p;
 
};
 
typedef struct s2 {
    
    int a;
    int b;
    int c;
    int *p;
 
} S2;
 
typedef struct s3 {
    
    int a;
    int b __attribute__ ((packed));
    int c __attribute__ ((packed));
    int *p __attribute__ ((packed));
 
} S3;
 
typedef struct s4 {
    
    int a;
    int b __attribute__ ((aligned (16)));
    int c __attribute__ ((aligned (16)));
    int *p __attribute__ ((aligned (16)));
 
} S4;
 
struct s1 strc_1;
 
struct s1 strc_1a[10];
 
S2 strc_2;
 
S2 strc_2a[10];
 
S3 strc_3;
 
S3 strc_3a[10];
 
S4 strc_4;
 
S4 strc_4a[10];
 
int main() {
 
    printf("sizeof struct s1: %zu\n", sizeof (struct s1));
    
    printf("sizeof strc_1: %zu\n", sizeof strc_1);
    
    printf("sizeof strc_1a: %zu\n", sizeof strc_1a);
    
    printf("sizeof *strc_1a: %zu\n", sizeof *strc_1a);
    
    printf("sizeof strc_1a[0]: %zu\n\n", sizeof strc_1a[0]);
 
    
    printf("sizeof (S2): %zu\n", sizeof (S2));
    
    printf("sizeof strc_2: %zu\n", sizeof strc_2);
    
    printf("sizeof strc_2a: %zu\n", sizeof strc_2a);
 
    printf("sizeof *strc_2a: %zu\n", sizeof *strc_2a);
    
    printf("sizeof strc_2a[0]: %zu\n\n", sizeof strc_2a[0]);
 
    
    printf("sizeof (S3): %zu\n", sizeof (S3));
    
    printf("sizeof strc_3: %zu\n", sizeof strc_3);
    
    printf("sizeof strc_3a: %zu\n", sizeof strc_3a);
 
    printf("sizeof *strc_3a: %zu\n", sizeof *strc_3a);
    
    printf("sizeof strc_3a[0]: %zu\n\n", sizeof strc_3a[0]);
  
 
    
    printf("sizeof (S4): %zu\n", sizeof (S4));
    
    printf("sizeof strc_4: %zu\n", sizeof strc_4);
    
    printf("sizeof strc_4a: %zu\n", sizeof strc_4a);
 
    printf("sizeof *strc_4a: %zu\n", sizeof *strc_4a);
    
    printf("sizeof strc_4a[0]: %zu\n\n", sizeof strc_4a[0]);
    
    return 0;
}
 
/*
output:
 
sizeof struct s1: 24
sizeof strc_1: 24
sizeof strc_1a: 240
sizeof *strc_1a: 24
sizeof strc_1a[0]: 24
 
sizeof (S2): 24
sizeof strc_2: 24
sizeof strc_2a: 240
sizeof *strc_2a: 24
sizeof strc_2a[0]: 24
 
sizeof (S3): 20
sizeof strc_3: 20
sizeof strc_3a: 200
sizeof *strc_3a: 20
sizeof strc_3a[0]: 20
 
sizeof (S4): 64
sizeof strc_4: 64
sizeof strc_4a: 640
sizeof *strc_4a: 64
sizeof strc_4a[0]: 64
 
*/
 

Comments