Check the size of the structure in C language

Introduction

I've been checking the size of the structure and have been curious about it, so make a note.

environment

OS:Linux(ubuntu64bit) Compiler: GCC

Checking the size of the data type

First check the size of the data type

size.c


#include <stdio.h>
#include <stdbool.h>
int main(void){
  printf("Size of each data type\n" );
  printf("char  :%zu\n",sizeof(char) );
  printf("short :%zu\n",sizeof(short) );
  printf("int   :%zu\n",sizeof(int) );
  printf("long  :%zu\n",sizeof(long) );
  printf("float :%zu\n",sizeof(float) );
  printf("double:%zu\n",sizeof(double) );
  printf("bool  :%zu\n",sizeof(bool) );
  return 0;
}

result

Size of each data type
char  :1
short :2
int   :4
long  :8
float :4
double:8
bool  :1

Also check the size of the pointer type

pointa.c


#include <stdio.h>
int main(void){
  printf("Pointer type size\n" );
  printf("char*  :%zu\n",sizeof(char*) );
  printf("void*  :%zu\n",sizeof(void*) );

result

Pointer type size
char*  :8
void*  :8

I won't write everything because it will be long, but all pointer types were 8 bytes.

Structure size

kouzoutai.c


#include <stdio.h>

typedef struct{
  short s;
  long l;
}kouzoutai;

int main(void){
  kouzoutai x;
  printf("x  :%zu\n",sizeof(x) );
  return 0;
}

Let's take a look at the size of x, which is kouzoutai. Will short (2) + long (8) = 10 bytes? result

x:16

It became 16 bytes. Why. This is because putting is secured.

What is putting

Members are placed in memory according to certain rules. It is placed in multiples of each data size. This is called alignment. (For 64bit) char: a multiple of 1 short: a multiple of 2 int: a multiple of 4 long: a multiple of 8

In this case

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
short Putting long

Since there is putting in 2 to 7 like this, it will be 16 bytes instead of 10 bytes. By the way, putting seems to mean padding and padding. The reason why there is putting is to make memory access efficient. It is that easy to find where it is for that multiple of the data size are arranged on the basis of certain rules.

kouzoutai.c


#include <stdio.h>

typedef struct{
  short s;
  long l;
}kouzoutai;

int main(void){
  kouzoutai x;
  printf("x  :%zu\n",sizeof(x) );
  printf("%p\n",&x.s );
  printf("%p\n",&x.l );
  return 0;
}

I added a statement to output the address of the member of the structure to kouzoutai.c earlier.

x  :16
Address of shorts:0x7ffebf1d8c90
address of long l:0x7ffebf1d8c98

If you look at the last digit of the address, you can see that short starts from 0 and long starts from 8.

Let's look at just one more point.

kouzoutai2.c


#include <stdio.h>

typedef struct{
  long l;
  long a;
  int s;
}kouzoutai2;

int main(void){
  kouzoutai2 y;
  printf("y:%zu\n",sizeof(y) );
  return 0;
}

This time all are likely to fit in multiples of the data size. The size of kouzoutai2 is Will it be long (8) + long (8) + int (4) = 20?

result

y:24
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
long long int Putting

Putting was secured up to 24 without ending with int. Putting seems to be ensured to fit the largest mold of the members in the structure. In this case, the largest member of kouzoutai2 is long, so putting is secured so that it becomes 24 in multiples of 8 that is 20 or more.

union

kyouyoutai.c


#include <stdio.h>

typedef struct{
  union{
    char c;
    long s;
  }q;
  long l;
  int i;
}kouzoutai3;

int main(void){
  kouzoutai3 z;
  printf("z:%zu\n",sizeof(z) );
  return 0;
}

If you simply count the size of the members, it is 32,

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
char Putting long long int Putting

This result is

z:24

Will be.

This happens because the char and long in the union are located at the same address. The image of union is as follows.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
long long int Putting
char

That's all for checking the size.

Points to be worried about

kouzoutai2.c


#include <stdio.h>

typedef struct{
  long l;
  long a;
  int s;
}kouzoutai2;

int main(void){
  kouzoutai2 y;
  printf("address of l:%p\n",&y.l );
  printf("Address of a:%p\n",&y.a );
  printf("address of s:%p\n",&y.s );

  int p;
  printf("Address of p:%p\n",&p );
  return 0;
}

When you do this

Address of l: 0x7ffe6cf682f0
Address of a: 0x7ffe6cf682f8
Address of s: 0x7ffe6cf68300
Address of p: 0x7ffe6cf682ec

Why does the address of p come before the address of l? Normally, the address of p would be "0x7ffe6cf68304" ... I'm curious.

Recommended Posts