Malloc Explained in 60 Seconds

preview_player
Показать описание
Malloc explained in 60seconds
Рекомендации по теме
Комментарии
Автор

Malloc explained in 3 words: “It allocates memory”

MIchaelArlowe
Автор

Freeing dynamically allocated memory is not good practice. It's REQUIRED.

rezamirhosseini
Автор

It's not a good practice but it's a must. The memory must be freed.

ohwow
Автор

you've piqued my curiosity. i've heard and understood freeing the memory when you're done with it in school, but i've never heard about setting the pointer to null.
is that just "idiot proofing" your code basically so you don't use it again accidentally?

jakubication
Автор

0:20 In C++ we usually do not do manual memory management... well Systems Programming is the exception, but for instance if you need mmap() you don't use malloc().
0:35 Actually, a slight (advanced-level) nitpick there: multiplication can overflow! The size_t type is just another unsigned type, and you typically expect the contract that "malloc() returns truthy pointer == enough memory has been found", so an overflow is both possible and a huge nuisance (it can result in buffer overflow). This is especially true for the common case where you have a length and an element size for an array you want to allocate, and you naively go and multiply them to get the total size, while in reality potential overflow should be checked against (and, if it would happen, allocation must fail because the very definition of size_t implies there can't be enough memory in such a case).
1:00 Using free() when it's time to is not just "good practice"; since C does not include a GC (it would defeat the efficiency of native executables), not freeing memory in time causes a memory leak.

erikkonstas
Автор

Nice! Good Explained in a short time ❤

muhammadfaizan
Автор

I did enjoy the video and I will watch more.

CXLVII
Автор

An important point about malloc is the data isn't initialiased in anyway so it could contain random data. You should always initialize the data after allocating it.

If you are allocating strings. You have to allocate one more byte than the size of the string you want to allocate and set the last byte allocated to null.

Also if you want to change the size of the block you allocated, there is realloc, again you have to check for null

pgpython
Автор

“With great power comes great responsibility”

BigJMC
Автор

I'm a computer science student who started with java and python, and my jaw dropped when I saw this. ABSOLUTELY INSANE. I CANNOT BELIEVE MY EYES AT SUCH BRILLIANT MALEVOLENCE CREATED BY THE C GODS. I'm genuinely going insane that this can exist I simply cannot believe my eyes.

grumpyyellowfang
Автор

What about allocation of memory when I don't know the number of data points? For example, storing user input in an array, say an integer array, in the case where I don't know how many integers the user will pass.

christosbinos
Автор

Pointers, assembly
Manage your memory with malloc() and free()

dl_supertroll
Автор

More interesting what happening behind the scenes

johnjosephlonergan
Автор

I don’t get what it means when it says allocates the bytes

justinadrowski
Автор

this was really good but i can hear all your mouth noises and breathe ins.

jeeperyeepr
Автор

This is my favorite c++ feature heart 😍 😍😍😍😍

blocks
Автор

int **matrix = (int **) malloc(ROWS * sizeof(int *));

for(int i = 0; i < ROWS; i++) {
matrix[ i ] = (int *) malloc (COLS * sizeof(int));
}

Good old days 😂

TheForge
Автор

Didnt even talk about memory framentation or page sizes. Epic fail.

gregorymorse
Автор

I've literally never found low level memory management to be difficult. Idk what people are complaining about

ZephyrCheez