Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Understanding Memory Layout of Structs | Structs and Memory
C Structs
course content

Зміст курсу

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

Understanding Memory Layout of Structs

Memory structures in the C programming language play a key role in understanding how data is stored and accessed in memory. When a structure is defined in C, the compiler determines how to place its members in memory based on alignment and padding rules.

Here's an overview of how basic memory allocation for structures works in C:

main

copy
123456789101112131415
#include <stdio.h> // simple struct struct Test { char x; // 1 byte int y; // 4 bytes }; int main() { struct Test example; printf("Size of struct Point: %zu\n", sizeof(example)); printf("Address of p.x (char): %p\n", &example.x); printf("Address of p.y (int): %p\n", &example.y); return 0; }

As expected, such a structure should occupy 5 bytes: 1 byte for int x, 4 bytes for int y, but this will be 8 bytes.

Why is the size of the structure much larger than we expected?

This occurs because the compiler can insert padding between members to ensure that each member begins at an address that is a multiple of its size.

Завдання

  1. Create a structure with four char fields;
  2. Initialize the structure and display its size in bytes;
  3. Display the address of each field.

Завдання

  1. Create a structure with four char fields;
  2. Initialize the structure and display its size in bytes;
  3. Display the address of each field.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 1
toggle bottom row

Understanding Memory Layout of Structs

Memory structures in the C programming language play a key role in understanding how data is stored and accessed in memory. When a structure is defined in C, the compiler determines how to place its members in memory based on alignment and padding rules.

Here's an overview of how basic memory allocation for structures works in C:

main

copy
123456789101112131415
#include <stdio.h> // simple struct struct Test { char x; // 1 byte int y; // 4 bytes }; int main() { struct Test example; printf("Size of struct Point: %zu\n", sizeof(example)); printf("Address of p.x (char): %p\n", &example.x); printf("Address of p.y (int): %p\n", &example.y); return 0; }

As expected, such a structure should occupy 5 bytes: 1 byte for int x, 4 bytes for int y, but this will be 8 bytes.

Why is the size of the structure much larger than we expected?

This occurs because the compiler can insert padding between members to ensure that each member begins at an address that is a multiple of its size.

Завдання

  1. Create a structure with four char fields;
  2. Initialize the structure and display its size in bytes;
  3. Display the address of each field.

Завдання

  1. Create a structure with four char fields;
  2. Initialize the structure and display its size in bytes;
  3. Display the address of each field.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 1
toggle bottom row

Understanding Memory Layout of Structs

Memory structures in the C programming language play a key role in understanding how data is stored and accessed in memory. When a structure is defined in C, the compiler determines how to place its members in memory based on alignment and padding rules.

Here's an overview of how basic memory allocation for structures works in C:

main

copy
123456789101112131415
#include <stdio.h> // simple struct struct Test { char x; // 1 byte int y; // 4 bytes }; int main() { struct Test example; printf("Size of struct Point: %zu\n", sizeof(example)); printf("Address of p.x (char): %p\n", &example.x); printf("Address of p.y (int): %p\n", &example.y); return 0; }

As expected, such a structure should occupy 5 bytes: 1 byte for int x, 4 bytes for int y, but this will be 8 bytes.

Why is the size of the structure much larger than we expected?

This occurs because the compiler can insert padding between members to ensure that each member begins at an address that is a multiple of its size.

Завдання

  1. Create a structure with four char fields;
  2. Initialize the structure and display its size in bytes;
  3. Display the address of each field.

Завдання

  1. Create a structure with four char fields;
  2. Initialize the structure and display its size in bytes;
  3. Display the address of each field.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Memory structures in the C programming language play a key role in understanding how data is stored and accessed in memory. When a structure is defined in C, the compiler determines how to place its members in memory based on alignment and padding rules.

Here's an overview of how basic memory allocation for structures works in C:

main

copy
123456789101112131415
#include <stdio.h> // simple struct struct Test { char x; // 1 byte int y; // 4 bytes }; int main() { struct Test example; printf("Size of struct Point: %zu\n", sizeof(example)); printf("Address of p.x (char): %p\n", &example.x); printf("Address of p.y (int): %p\n", &example.y); return 0; }

As expected, such a structure should occupy 5 bytes: 1 byte for int x, 4 bytes for int y, but this will be 8 bytes.

Why is the size of the structure much larger than we expected?

This occurs because the compiler can insert padding between members to ensure that each member begins at an address that is a multiple of its size.

Завдання

  1. Create a structure with four char fields;
  2. Initialize the structure and display its size in bytes;
  3. Display the address of each field.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 1
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt