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

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:

<pointer> = (data_type*)malloc(n * sizeof(data_type));
Note
Note

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

main.c

main.c

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.

Tâche

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:

  1. Use malloc() to allocate memory for one Student structure.
  2. Check if the allocation was successful (if not, return NULL).
  3. Assign a default name using sprintf() — for example, "Alice Johnson".
  4. Assign a value for age, such as 21.
  5. Assign a value for gpa, such as 3.85.
  6. Return the pointer to the allocated structure.

In the main function:

  1. Use the free() function to release the allocated memory after use.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

close

bookDynamically 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
Note

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

main.c

main.c

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.

Tâche

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:

  1. Use malloc() to allocate memory for one Student structure.
  2. Check if the allocation was successful (if not, return NULL).
  3. Assign a default name using sprintf() — for example, "Alice Johnson".
  4. Assign a value for age, such as 21.
  5. Assign a value for gpa, such as 3.85.
  6. Return the pointer to the allocated structure.

In the main function:

  1. Use the free() function to release the allocated memory after use.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
single

single

some-alt