Dynamically 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:
<pointer> = (data_type*)malloc(n * sizeof(data_type));
Note
When you dynamically allocate memory for use in your program, that memory remains reserved until you explicitly free it using free().
main.c
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.
Swipe to start coding
- Create a pointer variable and allocate memory for your structure;
- Display the size of structure;
- Fill the fields and display the content of your structure;
- Free up the allocated memory;
- Displaying the values again.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Résumer ce chapitre
Expliquer le code dans file
Expliquer pourquoi file ne résout pas la tâche
Awesome!
Completion rate improved to 4.17
Dynamically Allocating Structs
Glissez pour afficher le menu
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:
<pointer> = (data_type*)malloc(n * sizeof(data_type));
Note
When you dynamically allocate memory for use in your program, that memory remains reserved until you explicitly free it using free().
main.c
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.
Swipe to start coding
- Create a pointer variable and allocate memory for your structure;
- Display the size of structure;
- Fill the fields and display the content of your structure;
- Free up the allocated memory;
- Displaying the values again.
Solution
Merci pour vos commentaires !
Awesome!
Completion rate improved to 4.17single