malloc vs calloc Differences Explained | C Programming Tutorial

  Рет қаралды 33,675

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 50
@towtruckn
@towtruckn 2 жыл бұрын
Great video; the thing I find amazing is that even though using calloc() is at a drop in performance over the re assignment of one million integers; it still only took 1.6 thousandths of a second to do it which is still staggeringly quick and a testament as to how fast modern machines have actually become.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you Peter! :-) And I totally agree, a lot of lower level performance differences like malloc vs calloc almost don't even matter unless the numbers involved are huge or the operation happens "constantly" (e.g. like in a video game continually refreshing frames 60x per second).
@sshrek1996
@sshrek1996 Жыл бұрын
Thank you for explaining with example codes which majority of the channels don't do
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Shreyak! :-)
@goranosvald1792
@goranosvald1792 2 жыл бұрын
This was an amazing explanation, way better than my prof thank you!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Goran! :-D
@atamovassagi6563
@atamovassagi6563 3 жыл бұрын
hello sir as the first comment on this video first of all i wanted to thank u because of this tutorial .no matter how few your views are keep sharing videos like this 😊
@PortfolioCourses
@PortfolioCourses 3 жыл бұрын
Thank you! :-)
@aadhuu
@aadhuu 2 жыл бұрын
Wow! An amazing explanation indeed! Thanks a lot!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you Aditya! And you’re welcome. :-)
@TheCoder557
@TheCoder557 10 ай бұрын
a two year old video saved me. WOW great vid 👍🏾
@federicobau8651
@federicobau8651 Жыл бұрын
1+ sub. Great explanation and good example to deliver the concept
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed the explanation and example Federico, and welcome aboard! :-)
@Fillmore634b
@Fillmore634b Ай бұрын
I have a silly question - why do we fiil the heap thousand times? It's when we generate a random size and then allocate malloc of that size. Is this to fill the entire heap and leave no zeros because the heap is very big?
@kcvinu
@kcvinu 11 ай бұрын
Great video. I think I should need calloc when creating buffer to pass to Windows api functions, right ?
@vicsteiner
@vicsteiner 6 ай бұрын
first thank for the videos I am learning a lot through them! I did tried the clock part for this example and not sure why but I am getting in many runs a faster time for the calloc allocation. Any idea what might be going on? I am using gcc in an ubuntu os.
@PremiereStoss-qm9un
@PremiereStoss-qm9un 9 ай бұрын
Great video! While I get the concept of what you are doing here. I am a little confused on the "when" to use it. I mean, couldn't data always be there when using malloc? If so, wouldn't that always be a problem?
@someonespecial1329
@someonespecial1329 Жыл бұрын
I'm trying to run the code at 4:26 in linux (fedora) and the terminal outputs to me: malloc(): corrupted top size Aborted (core dumped) (Edit) If I run it again, I get other messages before Aborted, that are: double free or corruption (!prev) free(): invalid next size (normal)
@PortfolioCourses
@PortfolioCourses Жыл бұрын
It looks like you can get that first error if you overwrite the program memory or go past certain bounds, so maybe it's just allocating too much? www.reddit.com/r/cs50/comments/y7gppx/pset_5_malloc_corrupted_top_size_please_help/ Maybe try to make the numbers lower in terms of the number of times space is allocated and how much space is allocated?
@someonespecial1329
@someonespecial1329 Жыл бұрын
@@PortfolioCourses I tried debugging the code in vscode and the part that includes malloc seems fine, but the program aborts at the calloc statement (I toggled a breakpoint at that specific line).
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's odd, I'm not sure why that would be other than for some reason the memory is not available or something is going wrong when it's being allocated.
@shadowrealm8937
@shadowrealm8937 Жыл бұрын
please explain (toc-tic)/CLOCKS_PER_SEC what is this divider is and why it needed?
@bsbharat0705
@bsbharat0705 10 ай бұрын
i.e. clock cycle of the processor per second .. so when you devide ticks with that value you will get exact time in second ...
@pietraderdetective8953
@pietraderdetective8953 Жыл бұрын
hey i followed this tutorial, but in the tictoc example I always get 0.000000s both malloc and calloc. I had to increase the memory allocation for 1 trillion integer in order for it to show the time duration. but even so, the difference between malloc vs calloc in my laptop is 0.006s vs 0.005s. Has there been a major C release that made calloc as fast as malloc?
@jongecsek5085
@jongecsek5085 9 ай бұрын
Me too! My data at 1 trillion was malloc .003 and calloc at .004
@jongecsek5085
@jongecsek5085 9 ай бұрын
I'm on a desktop with a core i14 and DDR5
@vsantet42
@vsantet42 11 ай бұрын
I have always been confused about calloc. I wonder if calloc implementation is a bit more than just doing a malloc of ‘number * size’ then a ´memset’. That was my implementation for my computer science project lol
@theyayaa
@theyayaa 11 ай бұрын
Is the combination of malloc and memset better than calloc?
@quantumastrologer5599
@quantumastrologer5599 2 жыл бұрын
Great voice, great explanation
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you! :-)
@hesapörnek-u5q
@hesapörnek-u5q Жыл бұрын
hello you said you free the junk but then how come you fill up the heap with junk data ? I really didnt get that...
@Tugalou
@Tugalou 4 ай бұрын
When malloc is called, a chunk of memory is allocated and a pointer to the beginning of that memory is returned. When free is called, that pointer is removed and the memory is marked as available for use again. The data still exists as it was until overwritten, but you can't access it through traditional methods. It's like a house with a lock on it. When you have the key to the lock (pointer from malloc), you can easily go inside and see what's there. If you were to lose the key (call free), you couldn't go inside and see what's there. You can still see what was there through untraditional means, maybe looking through a window (malloc again and look at data that hasn't been reset), or digging through the floor (overflow exploits).
@ChrisNoesen
@ChrisNoesen 10 ай бұрын
Great video and explanation! I am binge watching all your videos!! One thing though, what is the downside to using mallac? I know you explain that data can be there but how does that interact with your code?
@PortfolioCourses
@PortfolioCourses 10 ай бұрын
If you were to use the memory that malloc() allocates with the assumption that it is set to zeros, that could be a problem that could cause a bug. Maybe you try to store counts of something, assuming the counts begin at 0, but then if the memory contained non-zero values like 4,5,etc, maybe your counts then incorrectly begin at those values. With malloc(), you would want to first set the memory you allocate to something before doing additional things with that memory. So the issue with malloc() leaving whatever is in memory there is only an issue if your code makes assumptions that it shouldn't or is written without this in mind. Thank you for binge watching the content. :-)
@ChrisNoesen
@ChrisNoesen 10 ай бұрын
@@PortfolioCourses Ok, that makes sense! Thanks! btw: I purchased your linked list course on Udemy. Great stuff! Your C examples and C tutorials are amazing. Keep em coming!!!
@PortfolioCourses
@PortfolioCourses 10 ай бұрын
Awesome thank you, I hope you like the course! :-D
@ironmonkey1990
@ironmonkey1990 10 ай бұрын
Thank you !
@FacundoTroitero
@FacundoTroitero 2 жыл бұрын
Nice. Where did you learn to explain things so easily and efficiently?
@FacundoTroitero
@FacundoTroitero 2 жыл бұрын
Nevermind, just visited your website, "20 years of teaching experience".
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I'm glad to hear that you enjoy the explanations. :-)
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thanks for visiting the website! :-)
@sleepysafron347
@sleepysafron347 2 жыл бұрын
First of all, amazing video. Also what IDE are you using or is it even an IDE? I’m new to coding so sorry for the dumb question.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I'm glad that you enjoyed the video! :-) I am using Visual Studio Code in this video together with Terminal on a Mac. Visual Studio Code is an extremely popular text editor with computer programmers, I recommend it as well.
@sandeepsiv
@sandeepsiv 2 жыл бұрын
Thanks ❤️
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Sandeep! :-)
@prodigysonhiddenforareason1239
@prodigysonhiddenforareason1239 2 жыл бұрын
Thanks boss!!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@ozangonul7286
@ozangonul7286 2 ай бұрын
thanks
@Mnogojazyk
@Mnogojazyk 2 жыл бұрын
Is there another memory allocation function that allows a specified value to be assigned to each memory address, i.e., a variant of calloc? Also, do calloc and malloc work on doubles, longs, structure, etc.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
No, there is just malloc, calloc, and then realloc for memory re-allocation. We could use malloc, and then memset() or memcpy() though to set the values of the block of memory that was allocated. And yes calloc and malloc will work with doubles, longs, structs and anything else really. :-)
realloc Basics
21:42
Portfolio Courses
Рет қаралды 16 М.
Dynamic Memory Allocation | C Programming Tutorial
31:51
Portfolio Courses
Рет қаралды 96 М.
I'VE MADE A CUTE FLYING LOLLIPOP FOR MY KID #SHORTS
0:48
A Plus School
Рет қаралды 20 МЛН
SLIDE #shortssprintbrasil
0:31
Natan por Aí
Рет қаралды 49 МЛН
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,5 МЛН
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
Dynamic memory allocation in C - malloc calloc realloc free
17:35
mycodeschool
Рет қаралды 891 М.
Dynamic Memory Allocation using malloc()
8:29
Neso Academy
Рет қаралды 506 М.
How to use realloc in C
11:56
CodeVault
Рет қаралды 10 М.
Dynamically Allocate An Array Of Structs | C Programming Tutorial
15:11
Portfolio Courses
Рет қаралды 37 М.
Dynamic Memory Allocation -- C++ Pointers Tutorial [8]
18:25
Professor Hank Stalica
Рет қаралды 30 М.
C_133 Dynamic Memory Allocation using malloc() | C Language Tutorials
23:44
Jenny's Lectures CS IT
Рет қаралды 221 М.
Dynamically allocated multi-dimensional arrays in C
14:17
CodeVault
Рет қаралды 45 М.
I'VE MADE A CUTE FLYING LOLLIPOP FOR MY KID #SHORTS
0:48
A Plus School
Рет қаралды 20 МЛН