For future videos, Cherno. 1:35 Stack and heap video 2:50 Why I don't like std namespace 7:40 The new keyword 10:10 Arrow operator 11:30 Smart pointer Now you can come back and put comments on your video. You're welcome :D - Love this series!
I think we'd try harder to motivate Cherno. I've tried really a little bit effort to be kinda RU-vidr more then 3 years before. At that time, I even only have had a freelancer job, which means I had a lot more time then now, but still can not easiely keeps that on. So may be we should build a much better community around / based on this series, and do use the patreon thing to support Cherno. (I've checked out that discordapp, but really not get the point easiely)
I've been learning C++ for more than a year now, and you've just made me have a revelation with this one. It litteraly made me stop the video, grab onto my forhead and shout "I GET IT NOW!!!" out loud. It's like I've ascended, opened my third eye, activated all my chacras and connected to the cosmose all at the same time. Thank you!
Lol this was literally me I never really understood the { type* name=new type } until I finally got the difference between stack and heap. And then it just clicked. It really feels amazing when the confusions fades away and you can finally answer your questions yourself.
I love the amount of videos you put out and I also love each single one of them! Keep up the great work, I'm so hyped for the in depth videos we are more and more getting in to!
Well, I even already "knew" all of the concepts so far, however it is a really nice reminder to see everything explained in such an order again. And for most of the parts you can learn something new too. Especially when it comes down to analyzing the generated assembly code underneath all the c++ stuff. Just to show what exactly happens internally. I love to know such things, without even considering if you might need them at any point. But when it comes down to optimization it is great to know how stuff will be like in assembly code so you can alter everything at your own pleasure.
Stack: → faster and better all around, if you can create an object on the stack then do it. → shorter lifespan then when allocated on the heap Heap: → If you need to extend the lifespan of the object outside of the function where it was defined, then allocate on the heap. → Use the new keyword to allocate on the heap → Anytime we use the new keyword, we must also use the delete keyword to clear that memory space when we are finished with the object.
This is probably the best series I've found so far on RU-vid among all the series for learning different languages. You make it so easy, and you give just the right amount of information so that we feel like we know just enough about what's going on under the hood without also feeling bogged down by too much technical information.
Cherno, I love your videos, anyways for completion of information, one of the main problems you may have forgot to mention about allocating on the heap with new operator, is that if an exception is thrown (and not properly caught) between the new and the delete operator, then the delete operator will never be called, thus never freeing that piece of memory from the heap. Thanks for the whole series tho, you made a wonderful job :)
Java/C#/Kotlin dev here. I also did my fair share of C, but never tried C++. Watching your videos now to learn a new language. The difference between "Entity()" and "new Entity()" has been an eye opener for me. It all makes so much more sense now! Nice job!
Seriously ur videos are the best in learning. Its more addictive than "The Breaking Bad". Also, I've to say the way of teaching is awesome. Video Quality and Audio Quality, Editing is awesome
I'm an experienced developer with some C experience (in the distant past). But I'm finding these videos very helpful in learning C++ - much more so than the book I was reading, A Tour of C++. Even when they cover familiar topics, I always learn something new. I especially appreciate that you occasionally break out of C++'s abstractions to discuss their lower-level implications. I also have to say, having watched some other programming videos, that I appreciate the lack of dumb jokes.
8:42 May not be entirely true since heap allocation is only a language specification for Java, if you looked at HotSpot you'll notice a lot of heap and stack operations are just abstractions to register based allocation at the lowest level (Java really hates allocating to anything besides registers and if it really has no other choice, the native thread stack, for some reason)
Cherno: "If you CAN create an object like this in the simplest possible manner, then do create an object like this!" The creators of the ridiculously complex framework I heavily depend on: "I will NEVER create an object like this."
I know most of C/C++, but your videos are soooo cool, fast and up to the point yet still very clear and understandable. Can you do a video about allocators (C++17 feature)?
That's not on the stack. That's put in the data/rodata section in memory. It gets constructed the first time the code flows through that function and gets destroyed when the program exists.
So does it mean that language like Java/C# allocates everything in heap and it would then also mean that CACHE miss is something that is default in Java/C# (Because they store nothing in Stack and hence caching the item is not possible) which makes them implicitly slower than C++. P.S: C# allocates struct in Stack and not in Heap
dude sometimes it's like you are deliberately making your room sound worse each video :D now you even have a super glassy resonance at 1k or some shit. i normally wouldn't mind but i know you are a musician. wouldn't be too complicated to put a dynamic eq on that recording. also you could turn up the saturation of your video material a bit. you always look so pale
if heap allocated objects do not have visibility restrictations then why Entity* entity = new Entity("Cherno"); inside inner curly brackets cannot be deleted with 'delete entity;' outside of those inner curly brackets ?
Tested the Entity pointer thingie at 6:33 as in the below, but it seems e2 still holds the name! Entity* e2; { Entity entity2("Entity2"); // created on the STACK e2 = &entity2; std::cout
Say I have a class called BMX055_Accel would the following code initialize a variable of type BMX055_Accel with the name accel? BMX055_Accel accel(i2c_bus_imu);
glad you mentioned to "hit the space bar"... it's one bug less to debug!!! ;-) ?sooo... a block of code { } automatically creates a stack frame?? did I already told your videos are the best??? ho... in the last 3 videos... ;-)) Ok.. I'm telling it again!!! ;-)
Hey guys, need your inputs here.. i'm replicating the same code from this video... but I get different output. your help is appreciated !! #include #include using namespace std; class Entity { private: string m_name; public: Entity(): m_name("Unknown"){} Entity(const string& name): m_name(name){} const string& GetName() const { return m_name; } }; int main() { Entity* e; { Entity ent = Entity("My name"); e = &ent; cout
finally i know how to do that class Location{} class Zone{ public: Zone(){ a[0]=new Location(4,2); b[1]=new Location(3,2); } Location** a=new Location*[20]; }
Java stores primitive type in stack, object in heap, though its not important since there are no pointer, JVM manages memory, and finalize(). C++ has to explicitly free memory. Python stores everything in heap except C functions.
Sorry if i am wrong and for my poor english .I thought we were overwriting a stackframe everytime because when you call a function this happens. -> push ebp (a general purpose register where you can save some value. the value gets saved so that it won't be overwritten) -> mov ebp, esp (copying the current stack address to that register which we moved so that we can come back to it) so now all the variables get allocated after this address, then after the function is done, this happens -> mov esp, ebp (NOTE: we saved the last address in ebp[ HERE is where the magic happens current address in esp will be overwritten with that old value we stored in ebp ]) -> pop ebp (restoring the value that was in the ebp before the function call )
So I'm understanding this right, you don't delete 'entity' because it went out of scope at the brace. But you can still delete 'e' because it's pointing to the right place on the heap?
:) I don't know what this hands stuff means. Really :). I was caught up in that stuff too. Until I was told about it. sounds funny what it can mean........ but then u do know :)
Maybe a silly question, but it not possible to have a constructor with default arguments in c++ ? If it actually is, is there any reason why Cherno didn't use that in his example?
I'm sry but as you created the object inside a function, I was so happy because that was exectly what I was looking for and you explained why this doesn't work. But my question if you can do it at all did not get answer and I got lost on the way through the tutorial.
instead of specifying "delete entity", could you have defined a destructor that deletes "this" and that way it is implicitly done any time you dynamically allocate space for instantiation of an object?
So the heap is like the pile of junk in the corner of the room. The stack is the things that sit on my desk. The difference is that I trash the things that sit on my desk when I'm done with them. This can't be said for the junk on the other side of the room.
you said stack allocated objects cannot be accessed elsewhere, but I stack allocated an object within a function, returned it and it works, they use the same memory address inside and outside the function. is this bad form?
I have found you are by far one of the best programming teachers on RU-vid. I was god awful at c++ and understood practically nothing until I started watching this series. Keep it up!
I love this series. You don't hesitate to go a bit deep in every video, even short ones, and I love it. I think the reason is that you are speaking with all your professional knowledge in heart, while many other videos are taught by academics.
could some one explain why my teacher prefers this method of using classes than the normal one and which one's better??? #include using namespace std; class sum{ private: int no1,no2,tot; public: void getdata(); void showdata(); }; void sum::getdata(){ cout
Hi Cherno, I have another question for you.... You showed two ways of scope instantiating objects. They are "ClassName objName(8)" and "ClassName objName = ClassName(8)". But quite often I see the following sintax: "ClassName objName = 8" Is this the same as the others?
Personal Notes: - Entity e(44); or Entity e = Entity(44); - Entity e; or Entity e = Entity(); but not Entity e = Entity; - Heap allocation might be necessary since stack is usually small - Heap allocation: Entity* e = new Entity(44); - Entity* e = new Entity() or Entity* e = new Entity both are allowed unlike stack allocation. - If not needed, use stack
This is amazing! You teach better than school! A quick question - What happens if you do this - Entity& GetEntity() { Entity* entity = new Entity(); return *entity; } int main() { Entity e = GetEntity(); std::cout
Soo, I'm a bit late to the party, but I do have a question: How do you handle heap de-/allocation within functions? Let's suppose you have a function that allocates memory on the heap and then returns a pointer to that object. Is there some convention to signal the user that "this function allocates memory, remember to free the result"? Is it generally considered bad to even do that? Another question I have: For my first job, we were mostly coding with ObjectPascal. It was considered a sin to not immediately follow up an instantiation with a try/finally that ensures the memory is always freed. Would you do the same in C++?
I did the option and went from c++ to c#, and I certainly remember wondering why all of a sudden EVERYTHING had to be instantiated with new which I rarely do in c++ lol
It all finally clicked when you mentioned the variable scope in the function, I remembered each call as a new stack frame and it all clicked... if I need something outside of this scope, I will require something stored on the heap... what about function returns? Coming from Java this was super Confusing until this video. Thanks Cherno
10:11 😎. I think you pretty tell the main reason why C++ is so powerful, but still has its hard time on adoption in companys. If I say writing Javascript is like driving a normal car, then writing Java & C# is driving sport cars, ==> writing C++ code is no more about car-driving, we talk about fight-jet controlling! 🎁🚀 Another neat tutorial, Thanks Cherno, you are great, RU-vid is great, C++ is great, I love the world we created in Computers.
Hi Cherno, thanks a lot for the video. I have a question for you. Is there any fundamental diference between instantiating an object with the "new" keyword like you did and with the "static" keyword like in "static ClassName objName"? Apart from in this second you can never delete the object, is there any other fundamental diference? I imagine that static variables are also stored in the heap. Aren't they?