You forgot to use a free() function for deallocation your dynamically allocated memory, as You said, the size of allocated memory does not have to be known at compile time, that means the compiler during the compile time does not know how much dynamically allocated memory have to been deallocated at the end of process/application, then you have to do it explicitly. But really nice explanation of that how it works, keep doing this videos 😉
You can also do a check for dynamically allocated memory using function exit(): if (pointer == NULL) exit(EXIT_FAILURE); so if your memory isn't allocated correctly (== NULL), you give exit function a constant "EXIT_FAILURE" to indicate failure and if you allocate memory in main() function, you can just write "return 1;" instead of using exit() I always do these checks, as well as freeing memory at the end of my program. Good habits :)
Great tutorial, although I think beginners may have a hard time identifying the difference. Perhaps you should have made the size a user input for the dynamic array.
I love your explanation it expands on what I learn in CS50X but honestly if I wasn’t familiar with arrays from David’s lecture I wouldn’t understand you.
I learned to always cast return value of malloc to data type I'm allocating memory for. Malloc returns pointer of void type (void*) and conversion from void to any type works as well as vice versa, but that's just mine usual practice because in C++ it's important to specify and make this conversion. In this case I'd just put --> int *my_array = (int*)malloc(sizeof(int) * 5);
Hey! I just found your channel and subscribed, love what you're doing! Particularly, I like how clear and detailed your explanations are as well as the depth of knowledge you have surrounding the topic! Since I run a tech education channel as well, I love to see fellow Content Creators sharing, educating, and inspiring a large global audience. I wish you the best of luck on your RUclips Journey, can't wait to see you succeed! Your content really stands out and you've obviously put in so much thought into your videos! I applaud you for that and really wish you the best for the future! Cheers, happy holidays, and keep up the great work :)
Great tutorial! Will the dynamically allocated array be initialized to 0 always? In my case, its taking random values unless assigned. So how do we initialize malloc( ) spaces to 0?
You can use calloc() which effectively does the same thing and only difference is that calloc() sets all bits of memory at 0, while malloc() doesn't do any sort of initialization.
In c++ If you write my_array[1] or 1[my_array] both will give you the same output no error message going to be displayed But I not sure about c language
Thnaks for these tutorials, they are very well explained. I have two questions: You refer to it as "C". Are you using this as interchangable with the term C++? And secondly, re the array, from other high level languages I know this as a 2 dimensional array. Is there a 3 dimensional array available within C (or C++)?
I see everyone declare the incremented variable **i** right before the loop instead of doing it inside the loop itself. Is there some performance benefit to this? Great video 👍
I’m a beginner so I don’t know for sure, but I’m gonna guess what the difference is. If you declare i only once, then the program doesn’t need to allocate memory for it everytime you want to use a for loop, giving it a *slight* speed boost. However, that means that the variable i lives throughout the entire scope of the main function and there needs to be room for it in memory constantly at run time. Declaring i inside the loop header means that i only lives within the for loop and the memory for it is freed upon exiting the loop. This means that the program needs to allocate memory for i everytime a loop is used, but on the flipside, i doesn’t occupy any memory except when it’s needed. So in summary, my guess is that declaring i once is slightly faster, while declaring i inside each loop is slightly slower but is slightly more efficient memory-wise. Although with the speed and size of RAM in devices nowadays, there probably isn’t any noticeable difference. Doing the former might be more convenient because there’s one less word you need to write in your for loop headers.
@@Sealedaway most for loops compile to LOOP instruction that will use ECX register as refrence for number of iterations, the value of *i* either lives there or it is pushed to the stacked when ECX is needed for some instruction than poped back in... however most compilers optimise vode nowdays and likely see *i* isnt refrenced anywhere after the for loop (outside of other for loops) and would just not track it for you...
@@qwerte6948 Yeah since that comment I’ve learned a lot more about compilers and just how much they often assume and intuit. In an overwhelming number of cases the only difference in the end result is just a shorter loop header. I guess it also has the benefit of never accidentally using the wrong integer type. For example if you’re looping through potentially huge arrays multiple times, accidentally typing int instead of size_t in a loop header could be disastrous, while declaring the iterator as size_t once will make sure it always has enough capacity. So in short, the performance will be the same except maybe for a few hyper specific cases, and the benefits are mostly just being slightly more concise and communicating intent.
you set the pointer the pointer my_array as malloc, i mean... you set something as the action of allocating memory? how that works? "what is your value?" "memory allocation" wth
So they are called "garbage", which are trace of old memory that used to store there, malloc() only tell that this memory region is in use, it doesn't initialize it to 0 or any values, that's your responsibility, you can use calloc which does but generally slower.
You forgot to use a free() function for deallocation your dynamically allocated memory, as You said, the size of allocated memory does not have to be known at compile time, that means the compiler during the compile time does not know how much dynamically allocated memory have to been deallocated at the end of process/application, then you have to do it explicitly. But really nice explanation of that how it works, keep doing this videos 😉
Yup! You're right, I'm already show casing bad coding practices xD thanks for letting me know.
You can also do a check for dynamically allocated memory using function exit():
if (pointer == NULL)
exit(EXIT_FAILURE);
so if your memory isn't allocated correctly (== NULL), you give exit function a constant "EXIT_FAILURE" to indicate failure
and if you allocate memory in main() function, you can just write "return 1;" instead of using exit()
I always do these checks, as well as freeing memory at the end of my program. Good habits :)
I watched noumerous of your videos and i love the thorough explaining that you do. Please don't change your swag. Thanks for the great videos!
Great tutorial, although I think beginners may have a hard time identifying the difference. Perhaps you should have made the size a user input for the dynamic array.
I love your explanation it expands on what I learn in CS50X but honestly if I wasn’t familiar with arrays from David’s lecture I wouldn’t understand you.
Tbf, he said you needed to have watched the first video or had familiarity with the syntax of C. So, this video is not for absolute beginners.
I learned to always cast return value of malloc to data type I'm allocating memory for. Malloc returns pointer of void type (void*) and conversion from void to any type works as well as vice versa, but that's just mine usual practice because in C++ it's important to specify and make this conversion.
In this case I'd just put --> int *my_array = (int*)malloc(sizeof(int) * 5);
You bring up so many amazing points beyond your great explanation of arrays. Big love ❤
Hey! I just found your channel and subscribed, love what you're doing! Particularly, I like how clear and detailed your explanations are as well as the depth of knowledge you have surrounding the topic! Since I run a tech education channel as well, I love to see fellow Content Creators sharing, educating, and inspiring a large global audience. I wish you the best of luck on your RUclips Journey, can't wait to see you succeed!
Your content really stands out and you've obviously put in so much thought into your videos! I applaud you for that and really wish you the best for the future!
Cheers, happy holidays, and keep up the great work :)
Thanks for your kind comment!
Good luck to you as well, and Happy Holidays!
@@LowLevelTV Yup, thank you!
Great tutorial!
Will the dynamically allocated array be initialized to 0 always? In my case, its taking random values unless assigned. So how do we initialize malloc( ) spaces to 0?
you can memset(my_array, 0, sizeof(int) * 5)
You can also use "calloc" instead of "malloc". Calloc will allocate the memory and zero the space at the same time.
You can use calloc() which effectively does the same thing and only difference is that calloc() sets all bits of memory at 0, while malloc() doesn't do any sort of initialization.
In c++
If you write my_array[1] or 1[my_array] both will give you the same output no error message going to be displayed
But I not sure about c language
Thnaks for these tutorials, they are very well explained. I have two questions: You refer to it as "C". Are you using this as interchangable with the term C++? And secondly, re the array, from other high level languages I know this as a 2 dimensional array. Is there a 3 dimensional array available within C (or C++)?
C and C++ are different languages.
I see everyone declare the incremented variable **i** right before the loop instead of doing it inside the loop itself. Is there some performance benefit to this?
Great video 👍
I’m a beginner so I don’t know for sure, but I’m gonna guess what the difference is. If you declare i only once, then the program doesn’t need to allocate memory for it everytime you want to use a for loop, giving it a *slight* speed boost. However, that means that the variable i lives throughout the entire scope of the main function and there needs to be room for it in memory constantly at run time. Declaring i inside the loop header means that i only lives within the for loop and the memory for it is freed upon exiting the loop. This means that the program needs to allocate memory for i everytime a loop is used, but on the flipside, i doesn’t occupy any memory except when it’s needed.
So in summary, my guess is that declaring i once is slightly faster, while declaring i inside each loop is slightly slower but is slightly more efficient memory-wise. Although with the speed and size of RAM in devices nowadays, there probably isn’t any noticeable difference. Doing the former might be more convenient because there’s one less word you need to write in your for loop headers.
@@Sealedaway most for loops compile to LOOP instruction that will use ECX register as refrence for number of iterations, the value of *i* either lives there or it is pushed to the stacked when ECX is needed for some instruction than poped back in... however most compilers optimise vode nowdays and likely see *i* isnt refrenced anywhere after the for loop (outside of other for loops) and would just not track it for you...
@@qwerte6948 Yeah since that comment I’ve learned a lot more about compilers and just how much they often assume and intuit. In an overwhelming number of cases the only difference in the end result is just a shorter loop header. I guess it also has the benefit of never accidentally using the wrong integer type. For example if you’re looping through potentially huge arrays multiple times, accidentally typing int instead of size_t in a loop header could be disastrous, while declaring the iterator as size_t once will make sure it always has enough capacity. So in short, the performance will be the same except maybe for a few hyper specific cases, and the benefits are mostly just being slightly more concise and communicating intent.
when i did this on code blocks it returned the opposite static vs dynamic...any clue as to what could have happened
you set the pointer the pointer my_array as malloc, i mean... you set something as the action of allocating memory? how that works?
"what is your value?"
"memory allocation"
wth
That's a bracket, not parentheses. The difference between braces, brackets and parens was taught day 1
Can someone explain why when i print out the dynamic array it seems to have random values in it?
So they are called "garbage", which are trace of old memory that used to store there, malloc() only tell that this memory region is in use, it doesn't initialize it to 0 or any values, that's your responsibility, you can use calloc which does but generally slower.
@Wutheheooooo Ohhh I see that makes sense thank you so much