Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Dynamic Memory Allocation | Dynamic Allocation and Multi-level Pointers
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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookDynamic Memory Allocation

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
some-alt