An introduction to using dynamic memory in C++. Concepts: Why we may need to allocate memory dynamically? new operator heap (free store) pointers to reference dynamically allocated memory delete operator garbage dangling pointers
Don't know if you are trolling or for real.. Universities exist to guarantee a certain quality standard, if you have a degree there is a guarantee that you meet those standards and know specific things.. There is no standard for people who learn online, they might be geniuses, but could also be "garbage" programmers.. ;)
@@DonSanchezDK but as an employer you can make interviews or tests ,if the employee pass ....that is a proof he know things necessary for the job...so there goes the purpose of college !
Zoran Jovanov Not really. It was replaced for a reason. Imagine having two functions, void Func(string* aStr) and an overloaded version called void Func(int aNum). If I call Func(0) or Func(NULL), Func(int aNum) will be called. But if we call Func(nullptr), then Func(string* aStr) will be called, which we want as we're dealing with pointers.
NULL is just constant of 0 difference is NULL is defined with #define and nullptr is a const variable it was replace just to tell you that you are assigning it to pointer and to avoid some memory leaks :)
Just started programming and c++, memory was the first topic that I would first struggle to understand and get my head around and this video really helped :) thank you so much
I've been reading Dr. Bjarne Stroustrup's book on Programming and referring to your videos often. I must say that I wouldn't probably be able to go through that complex book if it weren't for your clear, really lucid explanation of concepts. Thank you so much!
I removed a comment by mistake. Apologies to the user that left the nice comment. I had replied "Thanks!" to your comment under my other account and wanted to reply under my ReelLearning account. Deleting my comment, deleted your comment, for some reason.
i got the clear idea of what is heap and how to handle new/delete operators and that too dangling pointers and garbage collection. Thanks for this lecture.
This was the best educational video of explaining pointer and dynamic memory. Everything about pointer and dynamic memory is so much clearer now. Cheers !
The stack memory is managed by the program/compiler Dangling pointer = pointer that points to something that no longer exists. you can assign a dangling pointer the value NULL or 0 When a function exits its memory on the stack is deleted.
Thank you so much for the tutorial. Dynamic Memory Allocation has been kind of a plateau in my C++ learning, and you explained it very concisely. Very well done.
I'm actually kinda scared to start practicing on pointers... I don't really understand them to well...much reading up on to do. But the last example you did with the function sparked a question. If you close the program, then what happens to the garbage? Does it get deleted? Cleared?
When you start your program - OS allocates some memory for it, then your application messes with memory that it is given(storing everything it needs - text, globals, heap, and stack). After it terminates, OS is responsible for cleaning up the mess, so memory will be freed
1. Theoretically - yes, practically - not really. The address that contains garbage is still occupied, so nothing will be written there by the program or OS automatically unless you use a delete operator to tell that the memory address is now available for use (it doesn't really mean that the value is deleted, it might still be there it just marks the address as available and might or might not be used again). The only way to replace the value guaranteed is to get the address, and to do so you have to manually increment memory addresses and check whats in there, but another problem rises - how would you know what is a garbage value? For example 53453453453 can mean garbage or actual value, it can be an integer that fits entirely in a single memory address or can only be a portion of a large object that continues in the next address. That leads to your next question 2. This one is even more complicated, since it depends on implementation of memory management in OS, in general heap is not guaranteed to be contiguous, however more than often you will see the opposite if you write a program to demonstrate it (the program is tiny and will terminate quick so the whole heap will be within certain range). Also all(?) modern OS use virtual memory technique, meaning that the memory used by a process is different from actual physical addresses where stuff is stored, and on top of all that paging and swapping are used to move memory around. Hope that helps a little bit, also note that I am not a C++ guru and might be wrong on something. Please read more about terms I used in my answer to get a better undestanding
You dont have to start with pointers, they are not needed at all until well into mid-tier knowledge. When you need to move beyond the memory limit of a stack you will know 100% enough.
Thanks for the video its NUTS & BOLTS :). I am trying to learn how to make a program and everyone says C++ is powerful because of memory management, but now that I am learning it they keep getting away from the nuts and bolts and pretty soon everyone is talking about packaging everything into things already programmed in other things... boxes within boxes and which box to use... Before too long I don't even know what nuts and bolts are actually in the boxes or anything from anything. So thanks
this video explains alot. however, as a beginning, I still have a few questions.1. Variables stored in the stack DO have variable names and variables stored in the heap DO NOT have variable names, correct?2. What is the advantage/disadvantage of storing variables in the stack vs. in the heap?3. This is probably a stupid question, but why is it necessary to have a stack and a heap? Why can they not be considered one and the same? Then pointers would not be necessary?
I wish you referenced memory locations as an explicit hex value, such as "0x500" instead of "500". Instead, you have a bunch of ints (some that are actual int values and others that are actually memory addresses) all over the place and could confuse people.
History In 1955, Soviet computer scientist Kateryna Yushchenko invented the Address programming language that made possible indirect addressing and addresses of the highest rank - analogous to pointers. This language was widely used on the Soviet Union computers. However, it was unknown outside the Soviet Union and usually Harold Lawson is credited with the invention, in 1964, of the pointer.[2] In 2000, Lawson was presented the Computer Pioneer Award by the IEEE "[f]or inventing the pointer variable and introducing this concept into PL/I, thus providing for the first time, the capability to flexibly treat linked lists in a general-purpose high-level language".[3] His seminal paper on the concepts appeared in the June 1967 issue of CACM entitled: PL/I List Processing. According to the Oxford English Dictionary, the word pointer first appeared in print as a stack pointer in a technical memorandum by the System Development Corporation. en.wikipedia.org/wiki/Pointer_(computer_programming)#History
I'm extremely new to programming. But to summarize heap and stack, very simplistically, the stack is limited in terms of memory to the program. If for some reason you don't want to bog down your program by overusing your available stack memory, or risk overusing your memory and crashing the program via a stack overflow, assigning parts of the code to the heap temporarily allows you to avoid all of this? It's like having a glass, and you're limited to how much water you can pour in it, but if you need more water than your glass possibly can accomodate, you can pour it into another cup and sit it beside your main glass. Is this kind of accurate??? Thanks for the awesome video!!!!!!!!!
At 4:36 I am confused you said that you were dereferencing the pointer p when you were changing its value. Does the asterisk mean "dereference", I thought to "dereferencing" was only when you were getting the value of whats being pointed at, not reassigning its value. I'm not trying to be difficult I just don't know.
You should call this video "2 - Pointers and Dynamic Memory in C++ (Memory Management)" continuation of "1 - Call Stack, Scope & Lifetime of Variables"
Just a doubt, in the function x() when you don't use delete to deallocate memory in spite of being empty (because you just did new int, instead of new int(10)), it's not the same as if it wasn't allocated and that way stay free?, or exist some mechanism that prevents use that section of memory due to the lack of a delete statement for the same pointer that was declared with the new operator?, for the rest,it was completely pleasant to watch, thank you for share knowledge!, greetings from Perú.
Thank you so much for your effort man! your doing such a great job... I have only one question, is there any way to get your notes for the videos you ?
Great explanation. Is it possible to find size of the reserved memory for a specific variable in heap? For stack, it is sizeof() but what would be the case for heap????