#28: Dynamic Memory Allocation in C | C Programming for Beginners

preview_player
Показать описание
#28: Dynamic Memory Allocation in C | C Programming for Beginners

In this video, we will learn about dynamic memory allocation in C programming. More specifically, we will learn to allocate and destroy memory addresses while running the C program. Dynamic memory allocation allows us to allocate memory dynamically. C provides 3 major functions to perform dynamic memory allocation: malloc(), realloc(), and free(). We will now learn about each one of them with examples.

~
Resources:

Timestamps:
01:13 - malloc() function
02:56 - free() function
08:11 - realloc() function
11:04 - Programming Task
11:50- Quiz
~

Revise your learning using our C App

Find Programiz elsewhere:

#programiz #dynamicmemoryallocation #cprogramming #learncprogramming #codinginC
Рекомендации по теме
Комментарии
Автор

🔥Finding it Damn Hard to Understand C Programming?
Learn to code—the right way—with interactive lessons, quizzes & challenges. Build a strong programming base; it's IMPORTANT!

programizstudios
Автор

"Dynamic memory allocation allows us to allocate memory dynamically" 10/10 explanation.

LucasFCunha
Автор

The best for beginners is to read a complete tutorial. Without skipping important steps. It's important to UNDERSTAND how things work, not just make them work

CakeInvasion
Автор

Great videos, Computer science can get really confusing at parts, especially when you go from java/python to c, currently taking a course in college, and these videos have helped me greatly

nickralston
Автор

Your website has helped me a lot in my coding journey, thanks a lot for your contribution to the community.

irfanquader
Автор

i have a test on tuesday about this subject and you helped me a lot on learning it. i really hope that more and more people will be helped by this and the other videos of yours. God bless y'all and thx for the great content :D

guidoxenofonte
Автор

good explanation; B for the question asked last.

Tallaab
Автор

You are doing an amazing job . I have learned alot

videoventures
Автор

[ Programiz Quiz ]

Q. What is the correct way to allocate memory to store 12 double data?
a. malloc(20 * double)
b. (double*) malloc(12 * sizeof(double))
c. (double) malloc(20 * sizeof(double))
d. (double*) malloc( sizeof(20 * int))

programizstudios
Автор

Q. What is the correct way to allocate memory to store 12 double data?

A. malloc(20 * double)
B. (double*) malloc(12 * sizeof(double))
C. (double) malloc(20 * sizeof(double))
D. (double*) malloc( sizeof(20 * int))

programizstudios
Автор

mam, we should also use #include <stdlib.h> at the starting of code then only we are able to run the code

yogithabale
Автор

Thanks for the video, it helped a lot!

plankalkulcompiler
Автор

There's an error @ 8:15. "In our previous example we have allocated 400 bytes of memory to store 4 integer values." Each integer is four bytes, and thus you're allocating for four integers (4 bytes x 4 values) = 16 bytes of allocated memory.

brettchallice
Автор

I'm getting a warning for ptr = realloc(ptr, n * sizeof(int)); saying that realloc could return NULL, which "will cause the original memory block to be leaked", any know how to fix this?

errorhostnotfound
Автор

#include <stdio.h>
#include <stdlib.h>

int main() {

int n, i, *ages;

n = 4;

ages = (int*) malloc(n * sizeof(int));

if(ages == NULL){
printf("ERROR! Memory not allocated\n");
return 0;
}

printf("Input %d values: \n", n);
for(i = 0; i < n; i++){
scanf("%d", ages + i);
}

printf("Initial array elements: \n");
for(i = 0; i<n; i++){
printf("%d ", *(ages + i));
}

n = 6;

ages = realloc(ages, n * sizeof(int));

ages[4]=32;
ages[5]=59;

printf("\nFinal array elements: \n");
for(i = 0; i<n; i++){
printf("%d ", *(ages + i));
}

free(ages);

return 0;
}

tteokmember
Автор

ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT !

TheHeraldOfSpring
Автор

#include <stdio.h>

int main() {

int quantity = 4;

int* ages;

ages = (int*)malloc(quantity * sizeof(int));

if (ages == NULL) {
printf("Memory can`t be allocated");
return 0;
}
printf("Enter input values:\n");
for (int i = 0; i < quantity; ++i) {
scanf("%d", ages + i);
}
printf("Input values:\n");
for (int i = 0; i < quantity; ++i) {
printf("%d\n", *(ages + i));
}

quantity = 6;

ages = realloc (ages, quantity * sizeof(int));

ages[4] = 32;
ages[5] = 59;

printf("We adding new values:\n");
for (int i = 0; i < quantity; ++i) {
printf("%d\n", *(ages + i));
}

free(ages);

return 0;
}

countrysideshowyaigrock
Автор

Option B : (double*) malloc(12 * sizeof(double))



#include <stdio.h>

int main() {

int n = 4, i;
int* ages;

ages = (int*) malloc(n * sizeof(int));

if(ages == NULL) {
printf("Memmory Cannot Allocated");
return 0;
}

printf("Enter Four Ages:\n");

for(i = 0; i < n; i++) {
scanf("%d", ages + i);
}

printf("Initial Array Elements: \n");
for(i = 0; i < n; i++) {
printf("%d ", *(ages + i));

}

n = 6;

ages = realloc(ages, n * sizeof(int));

ages[4] = 32;
ages[5] = 59;

printf("\nFinal Array Elements: \n");
for(i = 0; i<n; i++) {
printf("%d ", *(ages + i));
}

free(ages);

return 0;
}

onic
Автор

When i run the realloc() code in visual studio code it returns an error as its expecting a void data type yet the malloc() worked.
Can anyone 'point' me in the right direction?

Thanks

int *ptr, i, n1, n2; 
ptr = (int*) malloc(n1 * sizeof(int));
ptr = realloc(ptr, n2 * sizeof(int)); // error expecting void type in ptr ?

laylalilly
Автор

programming task:
#include<stdio.h>
#include<stdlib.h>
int main(){
int n=4;
int* ages;
if(ages==NULL){
printf("memory is not allocated");
}

printf("enter input values: ");
for(int i=0;i<n;i++){
scanf("%p", (ages+i));
}
n=6;
ages=realloc(ages, n*sizeof(int));
ages[4]=32;
ages[5]=59;
printf("\nnewely assigned values: ");
for(int i=0;i<n;i++){
printf("\n%d", *(ages+i));
}
return 0;
}
quiz:option b is correct

sandeepvarma
visit shbcf.ru