Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Dynamic Memory Allocation | Dynamic Allocation and Multi-level Pointers
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Pointers Mastery

bookDynamic Memory Allocation

In C, arrays usually have a fixed size, meaning their length must be known at compile time. However, sometimes you need an array whose size is determined at runtime, for example, when reading user input or handling variable-sized data. This is where dynamic memory allocation comes in.

You can use functions from <stdlib.h> like malloc() or calloc() to allocate memory for arrays on the heap instead of the stack. This allows you to create arrays with flexible sizes that can grow or shrink as needed.

main.c

main.c

copy
12345678910111213141516171819202122232425262728
#include <stdio.h> #include <stdlib.h> int main() { int n; printf("Enter number of elements: "); scanf("%d", &n); // Allocate memory for n integers int *arr = (int*)malloc(n * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed!\n"); return 1; } // Assign values for (int i = 0; i < n; i++) arr[i] = i + 1; // Print values for (int i = 0; i < n; i++) printf("%d ", arr[i]); // Free allocated memory free(arr); return 0; }

Here, malloc() allocates a block of memory large enough to hold n integers. Always check if the pointer returned by malloc() is NULL, it means the allocation failed. After use, free the memory with free() to prevent leaks.

Note
Note

Always pair every malloc() or calloc() with a corresponding free() to avoid memory leaks and wasted system resources.

question mark

Which function in C allocates memory and initializes all elements to zero?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookDynamic Memory Allocation

Sveip for å vise menyen

In C, arrays usually have a fixed size, meaning their length must be known at compile time. However, sometimes you need an array whose size is determined at runtime, for example, when reading user input or handling variable-sized data. This is where dynamic memory allocation comes in.

You can use functions from <stdlib.h> like malloc() or calloc() to allocate memory for arrays on the heap instead of the stack. This allows you to create arrays with flexible sizes that can grow or shrink as needed.

main.c

main.c

copy
12345678910111213141516171819202122232425262728
#include <stdio.h> #include <stdlib.h> int main() { int n; printf("Enter number of elements: "); scanf("%d", &n); // Allocate memory for n integers int *arr = (int*)malloc(n * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed!\n"); return 1; } // Assign values for (int i = 0; i < n; i++) arr[i] = i + 1; // Print values for (int i = 0; i < n; i++) printf("%d ", arr[i]); // Free allocated memory free(arr); return 0; }

Here, malloc() allocates a block of memory large enough to hold n integers. Always check if the pointer returned by malloc() is NULL, it means the allocation failed. After use, free the memory with free() to prevent leaks.

Note
Note

Always pair every malloc() or calloc() with a corresponding free() to avoid memory leaks and wasted system resources.

question mark

Which function in C allocates memory and initializes all elements to zero?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 2
some-alt