How to use malloc to dynamically allocate memory

preview_player
Показать описание
Today we look at how to allocate memory using malloc and calloc, and how to manually remove this memory (as it's not cleaned up automatically) using the free function. Feel free to ask questions in the comments below!

---

Рекомендации по теме
Комментарии
Автор

This guy just explained this in 7 minutes better than 4 years of university I'm not even kidding

achillesa
Автор

The best absolute malloc video that I've ever saw! It takes books a full chapter to do what you did in less than 7 minutes.

benstone
Автор

Your videos are making my entrance into programming so easier, keep up the great work!

larsmichael
Автор

I really get amazed every time I see someone who's still working with C or trying to learn it.

Gupatik
Автор

Once again a very clear and easy to follow tutorial.

joelmanning
Автор

You can also access the elements by doing *(p+1) = 34; because p points to first address, if you add one, you will point to second

vasekharang
Автор

Thanks for the tutorial. Helping me start my C journey

gman
Автор

Awesome that I found this video the day before my exam lol, thanks! Great video :)

lauraborghijs
Автор

A very nice and clear explanation on malloc

TheeKariuki-vyub
Автор

let me call you "C MASTER"
thanks man

magedal-ward
Автор

Ty very much. I will be taking an exam on programming tommorow :)

Firebomber
Автор

I'm starting to learn C and I have a few questions about arrays, malloc and pointers and hope you could help me clear things up a little:
1) Is there some sort of problem if I copy an array with something like this:
int A[5]={1, 2, 3, 4, 5};
int B[sizeof(A)/sizeof(A[0])];
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++) {
B[i]=A[i];
}
Because, if I remember correctly, a code like that would be able to copy an array without using malloc, but I don't know if that would somehow create a problem in some case I'm not considering.

2) I've seen mentioned that it is necessary to always call free if you use malloc (I guess with calloc as well, but I don't know) so that memory can be reused for something else. Is it still necessary to free the memory even if the program is going to be terminated? In other words, would the allocated memory still be "locked", so to speak, for other programs even after program termination?

3) I see you're declaring an array using pointers. Is there a reason, other than maybe personal preference, to not declare it with something like int = p[3]; instead?

4) After you declared
int *p = (int*) malloc(sizeof(int));
what would happen if instead of assigning value to *p using
*p=5;
it was assigned without the *, that is to say, like this:
p=5;
I suppose it wouldn't work, but I'm still not quite sure why.

agsiar
Автор

great video, thank you, by the way, nice haircut bro

xinking
Автор

Thanks! That was really clear.
If you could do some longer video about double pointers and things like that you could be viral :)

tomasvenere
Автор

No need to free it. Memory leaks are fun

chudchadanstud
Автор

let's say on had malloc in a loop and it iterated along it's array and by the end of the loop the pointer address is near the end of the array. If I wanted to free the entire malloc(ed) memory, how can I do so by pointing back to the beginning of that array?

kevinklein
Автор

Hey! I'm trying to move away from scanf and wrote this to get some integers.
int main(){
char array[2][9];
int i;
for(i=0; i<2; i++){
fgets(array[i], 9, stdin);
}
int a, b;
a = atoi(array[0]);
b = atoi(array[1]);
(goes on...)
And I was thinking this is a waste of memory. I won't need those strings after the call to atoi, so I don't want to declare 'array' statically. But how would I get the size of a generic 2x9 char array so I can use malloc?

televisaoassassina
Автор

2:45 that's not true in operating systems at least. The memory will be freed by the OS when the process exits

Henrix
Автор

Following this tutorial gives me a seg fault when creating a larger pointer array

incrediblygay
Автор

just started watching the video, ngl he looks cute

Edit: Thanks! Good explanation :D

t_rm