Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Dynamically Allocating Structs | Pointers and Structs
C Structs
course content

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

bookDynamically Allocating Structs

Let's review how to dynamically allocate memory in the C language.

To allocate memory on the heap, use the malloc() function from the stdlib.h library:

Note

When you dynamically allocate memory for use in your program, that memory remains reserved until you explicitly free it using free().

c

main

copy
12345678910111213141516171819
#include <stdio.h> #include <stdlib.h> // structure definition struct Example { int someValue[20]; // int = 4 bytes, 20 * 4 = 80 bytes }; int main() { // allocating memory for Example structure struct Example* pExample = (struct Example*)malloc(sizeof(struct Example)); printf("Allocating memory = %zu bytes\n", sizeof(*pExample)); // freeing memory free(pExample); return 0; }

If a structure contains several fields of different data types, the compiler will equalize the size of the fields so that the structure is "conveniently" stored in memory.

After the completed job, pay attention to how many bytes are allocated for the structure with the int and char fields. This phenomenon will be discussed later in this course.

Task

  1. Create a pointer variable and allocate memory for your structure;
  2. Display the size of structure;
  3. Fill the fields and display the content of your structure;
  4. Free up the allocated memory;
  5. Displaying the values again.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
toggle bottom row

bookDynamically Allocating Structs

Let's review how to dynamically allocate memory in the C language.

To allocate memory on the heap, use the malloc() function from the stdlib.h library:

Note

When you dynamically allocate memory for use in your program, that memory remains reserved until you explicitly free it using free().

c

main

copy
12345678910111213141516171819
#include <stdio.h> #include <stdlib.h> // structure definition struct Example { int someValue[20]; // int = 4 bytes, 20 * 4 = 80 bytes }; int main() { // allocating memory for Example structure struct Example* pExample = (struct Example*)malloc(sizeof(struct Example)); printf("Allocating memory = %zu bytes\n", sizeof(*pExample)); // freeing memory free(pExample); return 0; }

If a structure contains several fields of different data types, the compiler will equalize the size of the fields so that the structure is "conveniently" stored in memory.

After the completed job, pay attention to how many bytes are allocated for the structure with the int and char fields. This phenomenon will be discussed later in this course.

Task

  1. Create a pointer variable and allocate memory for your structure;
  2. Display the size of structure;
  3. Fill the fields and display the content of your structure;
  4. Free up the allocated memory;
  5. Displaying the values again.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
toggle bottom row

bookDynamically Allocating Structs

Let's review how to dynamically allocate memory in the C language.

To allocate memory on the heap, use the malloc() function from the stdlib.h library:

Note

When you dynamically allocate memory for use in your program, that memory remains reserved until you explicitly free it using free().

c

main

copy
12345678910111213141516171819
#include <stdio.h> #include <stdlib.h> // structure definition struct Example { int someValue[20]; // int = 4 bytes, 20 * 4 = 80 bytes }; int main() { // allocating memory for Example structure struct Example* pExample = (struct Example*)malloc(sizeof(struct Example)); printf("Allocating memory = %zu bytes\n", sizeof(*pExample)); // freeing memory free(pExample); return 0; }

If a structure contains several fields of different data types, the compiler will equalize the size of the fields so that the structure is "conveniently" stored in memory.

After the completed job, pay attention to how many bytes are allocated for the structure with the int and char fields. This phenomenon will be discussed later in this course.

Task

  1. Create a pointer variable and allocate memory for your structure;
  2. Display the size of structure;
  3. Fill the fields and display the content of your structure;
  4. Free up the allocated memory;
  5. Displaying the values again.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Let's review how to dynamically allocate memory in the C language.

To allocate memory on the heap, use the malloc() function from the stdlib.h library:

Note

When you dynamically allocate memory for use in your program, that memory remains reserved until you explicitly free it using free().

c

main

copy
12345678910111213141516171819
#include <stdio.h> #include <stdlib.h> // structure definition struct Example { int someValue[20]; // int = 4 bytes, 20 * 4 = 80 bytes }; int main() { // allocating memory for Example structure struct Example* pExample = (struct Example*)malloc(sizeof(struct Example)); printf("Allocating memory = %zu bytes\n", sizeof(*pExample)); // freeing memory free(pExample); return 0; }

If a structure contains several fields of different data types, the compiler will equalize the size of the fields so that the structure is "conveniently" stored in memory.

After the completed job, pay attention to how many bytes are allocated for the structure with the int and char fields. This phenomenon will be discussed later in this course.

Task

  1. Create a pointer variable and allocate memory for your structure;
  2. Display the size of structure;
  3. Fill the fields and display the content of your structure;
  4. Free up the allocated memory;
  5. Displaying the values again.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt