new & delete Operators For Dynamic Memory Allocation | C++ Tutorial

  Рет қаралды 35,274

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 55
@mrtom-a-hawk6732
@mrtom-a-hawk6732 Жыл бұрын
The clarity of this video is beyond incredible. Fantastic work!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it! :-)
@TE_-.-
@TE_-.- Ай бұрын
yes how did you manage that
@BelegaerTheGreat
@BelegaerTheGreat 9 ай бұрын
My God, if I had just watched this 15 minute video before starting my project it would eliminate like 2 hours of confusion...
@dishmaster4140
@dishmaster4140 Жыл бұрын
From the bottom of my heart, thank you SO MUCH for this. I have been having the hardest time understanding pointers because no resource I had found was hitting quite the right spot on why we should care about pointers, but you did it. Thank you for taking the time to demonstrate the memory allocation and your attention to discussing that side of things. I have been pulling my hair out over this, and I cannot tell you how overjoyed I am to have found this video.
@nikolozjanikashvili9663
@nikolozjanikashvili9663 2 ай бұрын
your courses on C and C++ are the best on the internet. You explain everything perfectly. Thank you very much. Keep up!
@chickenmadness1732
@chickenmadness1732 Жыл бұрын
I love the commented diagram you made below the code. Really helped a bunch of stuff click.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed it! 🙂
@calengo454
@calengo454 Жыл бұрын
are you an angel? ive been trying to understand this for days and you're the only one that made it make sense
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm so glad to hear this video was able to help you out! :-)
@lexdose4104
@lexdose4104 Жыл бұрын
Ive been learning C++ as a 1st year CS student from around 5 months now and Im currently dealing with pointers from my book resources. I was very confused about the concept of new and delete operators and was wondering why I should use them and now Im starting to understand their functions and how important they are on memory management, thank you so much!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re very welcome I’m glad the video was helpful for you to learn these ideas! :-)
@SAURABHJAIN1989
@SAURABHJAIN1989 10 ай бұрын
7:14 int *ptr_to_int = new int(5); // to store an integar. double *array = new double[5]; //to store an array. So, round brackets ( ) to allocate single value and square brackets [ ] to allocate an array with the new operator. So, the basic syntax for the new operator is: datatype *var_name = new datatype(); or datatype *var_name = new datatype(val); //when we pass a value (might be an int or string or any other data type)
@uclocnguyenvo422
@uclocnguyenvo422 2 жыл бұрын
oh shit this tutorial is so good -_- u deserve millions of subscribers
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you for the positive feedback Đức! :-)
@muhammadluqman3452
@muhammadluqman3452 2 жыл бұрын
great playlist , you are a hidden gem!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you Muhammad! 😀
@mihaibozian
@mihaibozian 2 жыл бұрын
best explanation about dynamic mem allocation
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you for the positive feedback Bozian, I’m glad you enjoyed it! :-)
@skadasher5475
@skadasher5475 Ай бұрын
amazing work, thank you
@jojo-2059
@jojo-2059 4 ай бұрын
Let's say you have a 2D pointer array of objects and the pieces in the array is dynamically allocated. Why would it be a terrible idea to "delete" a specific entry, arr[i][j] and attempt to assign nullptr afterwards to that entry? I attempted that with a specific piece of code in my chess game in the one case it caused a segfault and the other case it functioned seemingly fine. I am new to C++ so this is a bit confusing.
@mouli4867
@mouli4867 Жыл бұрын
Thank for this tutorial. But I couldn’t find out a tutorial of yours on placement new. Please can we have a tutorial on that whenever you have time. Thank you once again.
@mirellesv
@mirellesv 2 жыл бұрын
great video! at 6:47, you have declared a pointer that stores the adress of the first element of an array, right? so, when assigning the values to the array, why couldn't we say, for example, *array[0] = 5?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Great question! 🙂You can use pointer notation with arrays but it looks a bit different than that, this video gets into that topic: kzbin.info/www/bejne/noiqinWDhJpoaaM.
@mirellesv
@mirellesv 2 жыл бұрын
@@PortfolioCourses this has been bugging me for a while 😅, I'll definitely watch it! thank you!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome! :-D
@msk1754
@msk1754 6 ай бұрын
Thanks for making video! If base adress is 0x0001, ptr_to_ have to be allocated 0x0001+ sizeof(int)?
@张武王
@张武王 10 ай бұрын
Just out of curiosity, what is the font used for coding in this video? It looks quite pleasant to me.
@tanang2565
@tanang2565 Жыл бұрын
truly appreciate your video ! Btw, can you link the document related to constructor syntax (name(name) {}) in 11:34 please ? I havent seen it before. Thank you.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's called a member initializer list, this video covers the concept: kzbin.info/www/bejne/jmLHeIiJi9OUrLM. There is some more 'official documentation' here: en.cppreference.com/w/cpp/language/constructor. 🙂
@Ivy891
@Ivy891 10 ай бұрын
amazing video !
@PortfolioCourses
@PortfolioCourses 10 ай бұрын
I'm glad you enjoyed it! :-)
@CPPLover-df9zw
@CPPLover-df9zw Жыл бұрын
Would it not be better to declare the big_array pointer out of the try-block and initialize it with a nullptr? After the allocation happens and the main terminates, this is going to result in a memory leak.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That example was to demonstrate memory allocation failure, so no memory was going to be allocated. If no memory is allocated, we don't need to delete/free the allocated memory. Also, when the main function terminates all memory is going to be "released". When the program stops executing there are no more memory leaks.
@i-yusuf
@i-yusuf 2 жыл бұрын
Hey man, thanks for making c++ playlist, Which IDE are you using ?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome Yusuf! I am using Xcode on a Mac in most videos, though sometimes I use Visual Studio Code and the terminal/compiler instead.
@korngsamnang
@korngsamnang 2 жыл бұрын
int x = 5; int *y = &x; //s1 int *y = new int(x); //s2 Sir, what is difference between s1 and s2 ?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
s1 will have y be a pointer to variable x. In other words, y will store the memory address that the variable x is located at. x is located in a place in memory called the stack. s2 uses dynamic memory allocation with the new operator. The new operator will create space to store a new int value, in a place in memory called the heap. That new int value will be set to whatever value x is set to, in this case 5. This new int value will have no relationship to x though. The value of x is used to set this new int value, that's all. And y will store the memory address of this new int value on the heap.
@korngsamnang
@korngsamnang 2 жыл бұрын
@@PortfolioCourses Thank you sir for clarification.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@korngsamnang You're welcome! 😀
@Vichained
@Vichained 2 жыл бұрын
didnt you cover this topic in previous videos?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Nope, I’ve used them in previous videos but haven’t been able to cover them yet. I’ve been hoping to cover them for awhile now.
@Vichained
@Vichained 2 жыл бұрын
@@PortfolioCourses I see. Btw, could you cover Maps in the short term? Idk if they are on your planed videos (I guess), but it would be a very interesting topic along with unique_ptrs and stuff. Good Video again Kevin!. Keep up with the good content!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I’m not sure when I’ll cover Maps but hopefully one day I’ll get to it all. :-)
@YeabsiraAlemu-bx9si
@YeabsiraAlemu-bx9si Жыл бұрын
Fantastic
@stonedcodingtom9097
@stonedcodingtom9097 2 жыл бұрын
Thanks!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re very welcome! :-)
@ivans7
@ivans7 2 жыл бұрын
What is the editor name?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
In this video I am using Xcode on a Mac. :-)
@jinuraj1711
@jinuraj1711 2 ай бұрын
i don't believe in god, but definitely you are one such!
@masihaahmadi7663
@masihaahmadi7663 Жыл бұрын
great
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m glad you enjoyed it! :-)
@imveryhungry112
@imveryhungry112 Жыл бұрын
im too dum to understand any of this
@Daniel-vs7it
@Daniel-vs7it Жыл бұрын
(If im not wrong) we need to make this a pointer to point to the address of the int, it's like if we were making int* ptr = &var but instead of using a already initialized variable we create one with the new keyword, the pointer just points to the address of the new dynamically allocated variable, after we finish we need to use delete because if we dont do it we'll cause a memory leak.
@guillaumemz
@guillaumemz 5 ай бұрын
Please do NOT use new/delete to dynamically allocate/deallocate memory in production code. Use smart pointers (std::unique_ptr, std::shared_ptr with the helper functions std::make_unique and std::make_shared) instead. By the way, avoid using namespace std; and use nullptr instead of NULL.
Deleted Functions | C++ Tutorial
5:56
Portfolio Courses
Рет қаралды 3,7 М.
Dynamic Memory Allocation -- C++ Pointers Tutorial [8]
18:25
Professor Hank Stalica
Рет қаралды 30 М.
I Sent a Subscriber to Disneyland
0:27
MrBeast
Рет қаралды 104 МЛН
Маусымашар-2023 / Гала-концерт / АТУ қоштасу
1:27:35
Jaidarman OFFICIAL / JCI
Рет қаралды 390 М.
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,5 МЛН
The NEW Keyword in C++
10:53
The Cherno
Рет қаралды 252 М.
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 854 М.
this Keyword And Use Cases Explained | C++ Tutorial
10:30
Portfolio Courses
Рет қаралды 8 М.
Dynamic Memory Allocation | C Programming Tutorial
31:51
Portfolio Courses
Рет қаралды 97 М.
Learn Any Programming Language In 3 Hours!
22:37
Code With Huw
Рет қаралды 632 М.
Pointers and Dynamic Memory in C++ (Memory Management)
13:54
ReelLearning
Рет қаралды 380 М.
how Google writes gorgeous C++
7:40
Low Level
Рет қаралды 990 М.
new OPERATOR IN C++ - 47
8:01
Codearchery
Рет қаралды 71 М.
I Sent a Subscriber to Disneyland
0:27
MrBeast
Рет қаралды 104 МЛН