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));
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
You need to create a Student structure and dynamically allocate memory for it using the malloc() function.
Your task is to initialize the structure with sample values and return a pointer to it.
Inside the createStudent function:
- Use
malloc()to allocate memory for one Student structure. - Check if the allocation was successful (if not, return
NULL). - Assign a default name using
sprintf()β for example,"Alice Johnson". - Assign a value for
age, such as21. - Assign a value for
gpa, such as3.85. - Return the pointer to the allocated structure.
In the main function:
- Use the
free()function to release the allocated memory after use.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain why the compiler equalizes the size of structure fields?
How do I check the size of a structure in C?
What happens if I forget to free dynamically allocated memory?
Awesome!
Completion rate improved to 4.35
Dynamically Allocating Structs
Swipe to show 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));
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
You need to create a Student structure and dynamically allocate memory for it using the malloc() function.
Your task is to initialize the structure with sample values and return a pointer to it.
Inside the createStudent function:
- Use
malloc()to allocate memory for one Student structure. - Check if the allocation was successful (if not, return
NULL). - Assign a default name using
sprintf()β for example,"Alice Johnson". - Assign a value for
age, such as21. - Assign a value for
gpa, such as3.85. - Return the pointer to the allocated structure.
In the main function:
- Use the
free()function to release the allocated memory after use.
Solution
Thanks for your feedback!
single