The NEW Keyword in C++

  Рет қаралды 240,259

The Cherno

The Cherno

Күн бұрын

Patreon ► / thecherno
Twitter ► / thecherno
Instagram ► / thecherno
Discord ► / discord
How to create objects ► • How to CREATE/INSTANTI...
Series Playlist ► • C++
Gear I use:
-----------------
BEST laptop for programming! ► geni.us/pakTES
My FAVOURITE keyboard for programming! ► geni.us/zNhB
FAVOURITE monitors for programming! ► geni.us/Ig6KBq
MAIN Camera ► geni.us/t6xyDRO
MAIN Lens ► geni.us/xGoDWT
Second Camera ► geni.us/CYUQ
Microphone ► geni.us/wqO6g7K

Пікірлер: 283
@nazar1744
@nazar1744 6 жыл бұрын
new: hey pls give me 4 bytes Standard-Library: no! get a job !
@exoticcoder5365
@exoticcoder5365 3 жыл бұрын
😂
@erwinschrodinger2320
@erwinschrodinger2320 3 жыл бұрын
Lol
@germinatorz
@germinatorz 6 жыл бұрын
Best C++ series on KZfaq!
@Gilpow
@Gilpow 6 жыл бұрын
Useless comment stating the obvious
@jscorpio1987
@jscorpio1987 4 жыл бұрын
Gilpow he’s just being a nice guy and giving Cherno a compliment. Which is something we need more of in this cold, heartless world.
@garyhahaha5380
@garyhahaha5380 4 жыл бұрын
there isn't any real competition!!!!
@xcaliber6027
@xcaliber6027 4 жыл бұрын
@gilpow check thenewboston, you will be forced to take back your words.
@pierfrancescopeperoni
@pierfrancescopeperoni 3 жыл бұрын
@@jscorpio1987 *brainless world.
@michaelwright8576
@michaelwright8576 3 жыл бұрын
Summary: → Use the new keyword to allocate a specified memory size on the heap in bytes (ex. Int would take 4 bytes of memory) → operating systems must then go and find a contiguous block of memory to match the requested variable type and return a pointer to that memory address. → With a class, it will determine the size of the class and then find a memory allocation → with arrays it will calculate the type of array and multiply by the size of the array. //int * 50 = 200 → The new keyword not only allocates memory, but also calls the constructor → don't forget to use the delete keyword when we use the new keyword. → if we use new keyword with array like new int[], use delete keyword like delete[] → use placement new to i.e new(b) to choose the location of where the memory is stored
@ultimatedragon4281
@ultimatedragon4281 2 жыл бұрын
→The delete keyword also calls the destructor.
@user-kj6lj7dp9j
@user-kj6lj7dp9j Ай бұрын
best comment
@saisurisetti6278
@saisurisetti6278 4 жыл бұрын
"Why do you want to learn C++... unless you specifically care about performance, or you specifically care about everything that goes on?" - Cherno the great.
@maybe_raj
@maybe_raj 4 жыл бұрын
- The* Charno* The Great! xD
@awesomedavid2012
@awesomedavid2012 4 жыл бұрын
It's really true if you just want something that works use python or some other language. Coding isn't about "pick your favourite language and stick to it". You pick the language that's best for what you're doing
@darkbolt333
@darkbolt333 4 жыл бұрын
Sai Surisetti I think I don't fit the criteria... I started learning C++ because it has more data structures than Python, it's a common language and Python hates recursive functions. Python has that nasty recursion limit of 1000. And even if I increase that limit (with setrecursionlimit function), it won't go to 50,000.
@damnstupidoldidiot8776
@damnstupidoldidiot8776 4 жыл бұрын
@@darkbolt333 I started learning C++ because it worked with hardware the best, for arduino. But then when I really got started with C++ I loved the syntax and I hate the Python syntax.
@jasperbutcher2596
@jasperbutcher2596 4 жыл бұрын
just here to learn the syntax for fun idk might be going to jail now
@chrisstone-streetlightinte5629
@chrisstone-streetlightinte5629 6 жыл бұрын
I've spent hundreds of hours learning to program and this series, by far, has some of the best explanations of how C++ actually works. Keep it up!
@papinkelman7695
@papinkelman7695 6 жыл бұрын
"String 28 bytes... just guessing from memory" So, String guess* = new guess(); You guessed from your heap?
@vanDalle
@vanDalle Жыл бұрын
Man, you are awsome. I've dropped on one of your films on YT and now I'm in the middle of C++ series, because in 5 min. you are explaining dozens of pages from my book and you're doing it in very easily understandable way. Thanx for your work. All the best from Poland.
@muhammadammar6661
@muhammadammar6661 Жыл бұрын
I cant believe someone can actually explain things like so good. Brilliant job. Much appreciated.
@Djzaamir
@Djzaamir 6 жыл бұрын
Dude , this is my game of thrones now a days , thanks for such an amazing series :)
@MirrorsEdgeGamer01
@MirrorsEdgeGamer01 6 жыл бұрын
_"Please give it to me"_ That's what she said. _0:24__ _"It is actually quite deep."_ That's what she said.
@valizeth4073
@valizeth4073 6 жыл бұрын
I love your videos, even though I pretty much know C++, it's always good to watch beginners guide videos, there's always things you've missed.
@Raymoclaus
@Raymoclaus 6 жыл бұрын
Welcome to the C++ series where "we'll talk about it in a future video"
@Banom7a
@Banom7a 6 жыл бұрын
We talk about in soon™
@TheCherno
@TheCherno 6 жыл бұрын
One day it will be complete!
@h.hristov
@h.hristov 6 жыл бұрын
Don't give him a hard time :D, I'm sure he'll go over the advanced topics once he's gone through the basics. As he said, he's trying to make the series as perfect as he wants
@Banom7a
@Banom7a 6 жыл бұрын
no worry, take your time, excellent video as usual
@LucidStew
@LucidStew 6 жыл бұрын
..or he could make one 10 hour video that no one would watch and get it all in one shot.
@pacsmile
@pacsmile 6 жыл бұрын
Love your videos, i've been learning quite a lot and using it for embeded c++ programming, keep up the great work!
@suyash.01
@suyash.01 3 жыл бұрын
beautiful expatiation. super straightforward and clean and simple.
@WeberJ81
@WeberJ81 5 жыл бұрын
@TheChernoProject I admire the videos, really simplifies C++ and i'm still learning new things about it from these, also big fan of the ibanez in the background, just don't lean it on the stock, it warps the neck, guitar teacher playing 26 years/Senior Software Engineer to C++ teacher tips :), thanks for the videos
@theglitchmindset2256
@theglitchmindset2256 3 жыл бұрын
Cherno is saving me so much time effort MONEY and energy really appreciate you man. i starting this series September 2020 almost halfway there
@tef6477
@tef6477 6 жыл бұрын
Thank you so much . Was so used to malloc(sizeof)). Keep it up mate .
@leixun
@leixun 4 жыл бұрын
*My takeaways:* 1. The main purpose of *new* is to allocate memory for the user's need on the heap 1:25 2. MUST use *delete* with new 7:45
@samruddhipaiyawal5586
@samruddhipaiyawal5586 4 ай бұрын
wow that was actually very informative!! thankyou!
@BlackJar72
@BlackJar72 6 жыл бұрын
Well, I'm watching these videos because you often throw interesting and potentially useful detail most books don't mention (and my one actual class didn't either).
@kenamreemas3295
@kenamreemas3295 5 жыл бұрын
As soon as I completed watching this vid, I tried to use delete and see if calls the destructor.. and it DOES!!
@haykav
@haykav 6 жыл бұрын
When you opened up the definition for new and I saw that it's just an operator for giving a pointer the required amount that it would neeeeeeed to stoooreee on the heap, I was like OH MY GAWDDD, My mind has been blown!!
@motbus3
@motbus3 4 жыл бұрын
i know somewhat of cpp but i was watching the full series for a review... and i today i learned that new can have a specific allocation address... thats so cool
@h.hristov
@h.hristov 6 жыл бұрын
Thanks yet again for the awesome video!
@oni_thefirst
@oni_thefirst 4 жыл бұрын
I like how he used new for like 6 videos in a row before noticing that he never explained what it is. Great tutorials either way
@ibiixie
@ibiixie 6 жыл бұрын
The standard library looks so complicated lol
@alecmather
@alecmather Жыл бұрын
This series is seriously so addicting
@mmaranta785
@mmaranta785 3 жыл бұрын
Thanks for your help. Your videos are very helpful.
@stercolaireotwo9285
@stercolaireotwo9285 6 жыл бұрын
Thanks for your tutorial. Are you planing to teach some optimizations in object instantiation frecuently used in game engines (such as object pools and instead of allocating and instantiating N objects separately, allocate the memory those N objects require at once and then instantiate all the objects in that chunk of memory)?
@adityagoyal6964
@adityagoyal6964 3 жыл бұрын
You are insane cherno. After 1 month i only open my instagram account just to follow you. Keep up the good work and you will surely get that love what you deserve❤❤❤❤❤
@umairalvi7382
@umairalvi7382 4 жыл бұрын
Heyy cherno a big fan of yours im watching your c++ series for quite a long time and in one video you said that you're gonna come with a project in c++ very soon.when are you going to come with that project its been a long long time please upload it as soon as possible it will be extremely helpful. Thank youu
@Destroyer19941995
@Destroyer19941995 3 жыл бұрын
Thank you for teaching me this.
@Basel-ll8fj
@Basel-ll8fj 2 жыл бұрын
Best C++ series ❤❤
@jeeperscreeperson8480
@jeeperscreeperson8480 2 жыл бұрын
Heap allocation is slow mainly because the kernel has a lot of stuff to do besides fulfilling your memory requests. I believe the kernel also sleeps for a fixed period of time every so often. Also, because the kernel can supply you with a chunk of memory of any size, the heap is fragmented. Idk how the windows kernel handles defragmentation, but I assume it defragments memory every time you ask for a big chuck. All the more reasons to ask as few times as possible.
@krisitak
@krisitak 4 жыл бұрын
There is a difference between the "new expression" and "operator new". Operator new is pretty much equivalent to malloc except it can be overloaded/specialized. The "new expression" picks an appropriate operator new to call which returns a block of memory and then the constructor is called on the memory returned by the operator.
@NexGenSlayer
@NexGenSlayer 6 жыл бұрын
Please do a video on linked lists and nodes! These are really important and pretty confusing
4 жыл бұрын
Those are actually about data structures, it's not something about the C++ itself.
@sebastiangudino9377
@sebastiangudino9377 2 жыл бұрын
I wouldn't say they are "really important". Yes, they are a phenomenal way to implement sequential data when you know you will be performing many insertions and concatenation. As well as when you want to implement something like a tree. But in day to day you are either going to use them because they are your language's preferred way to handle lists, like Haskell or Scala. Or you are not really going to use them very much. They actually tend to get overused by beginners. Since when all you have is a hammer everything looks like a nail. Most of the time a Dynamic Array (So a Vector in C++) is what we really want. You wouldn't default to storing you data in a linked list unless you have a particular optimization or algorithm in mind you would want to use
@user-wl3nd6si9l
@user-wl3nd6si9l 6 жыл бұрын
Does delete call destructor?
@TheCherno
@TheCherno 6 жыл бұрын
+Роман Капуста yes
@halemm
@halemm 6 жыл бұрын
hey cherno, where did you learn all this stuff, at work or at university? thanks!
@tmvkrpxl0135
@tmvkrpxl0135 3 жыл бұрын
I get a weird feeling from your videos, I thought I know well enough about some topic but there's always something more when I watch your videos
@thomiadityaalhakiim5831
@thomiadityaalhakiim5831 2 жыл бұрын
ok let me save this playlist on the heap
@AlbertoRivas13
@AlbertoRivas13 4 жыл бұрын
very useful sir thank a lot
@ohwow2074
@ohwow2074 3 жыл бұрын
Jesus!! This video was soooo informative!!!
@omerkaragoz
@omerkaragoz 6 жыл бұрын
Why there is a second const after GetName() on line 14?
@VoidAshen
@VoidAshen 3 жыл бұрын
being anxious with his hand near the cactus
@soulimanemammar2909
@soulimanemammar2909 2 жыл бұрын
There is something else to mention: when we call the new operator, it's actually a syscall (in terms of operating systems) and when a syscall happens our program loses control (the os regains the control to fulfill the request -in our case memory allocation-) and it's up to the os scheduler to decide which process will be given the control once the syscall handling finished (and most of the time it's not our program that will regain control)
@Usammityduzntafraidofanythin
@Usammityduzntafraidofanythin 2 жыл бұрын
Well unless the program is selected by the user at the time. I read that every process in the computer is being maintained by the cpu just flopping through them very very quickly over and over; programs unselected by the user are just put on pause if they have that feature in them, I guess.
@batteryjuicy4231
@batteryjuicy4231 2 ай бұрын
this was very helpfull keep up the good job. I love this series! It has helped me A LOT!
@paherbst524
@paherbst524 2 жыл бұрын
question: is there a way to profile where in an application is calling new?
@kenan2386
@kenan2386 2 жыл бұрын
It basically allocates something properly
@RobertMilat
@RobertMilat 2 жыл бұрын
The most memorable part of this video is that malloc sounds very evil to me
@hubtubsub5209
@hubtubsub5209 2 жыл бұрын
Just like new calls malloc internally, when we call delete does it internally calls *free* too?
@shahidk2223
@shahidk2223 6 жыл бұрын
please make a video about cstrings and lpcstr
@joseph555able
@joseph555able 4 жыл бұрын
Finally!! Thank you
@ScienceMarcYT
@ScienceMarcYT 6 жыл бұрын
I'm assuming the memory gets freed when the program ends if you don't call delete?
@Crossbow123
@Crossbow123 6 жыл бұрын
When allocating an array like new Entity[n], does the constructor get invoked as well for each object?
@Crossbow123
@Crossbow123 6 жыл бұрын
Thanks. Good point =)
@anassqadil1376
@anassqadil1376 2 жыл бұрын
Bro U are a legend Thanks
@raht9578
@raht9578 3 жыл бұрын
THANK YOUUU
@dx2002sg
@dx2002sg 3 жыл бұрын
wow definitely deeper than i thought for this "new"
@strangeruler2317
@strangeruler2317 2 жыл бұрын
For people wondering what the variables contain could be if malloc function wont call constructer: Constructer intializes the data so, malloc allocated data supposed to untialized and should show random values i.e Int *a = malloc(sizeof(int)); cout
@LucidStew
@LucidStew 6 жыл бұрын
hair is too neat.
@samdavepollard
@samdavepollard 3 жыл бұрын
If you're new, use new. None of that malloc malarkey. Difficult to believe we're not even halfway through this series yet! Many Thanks for sharing your knowledge.
@nukeulater
@nukeulater 6 жыл бұрын
nigga these tuts are god's work! good job (i just found you :D), i understood more c++ than i do at school, keep it up
@Usammityduzntafraidofanythin
@Usammityduzntafraidofanythin 2 жыл бұрын
It is incredible how powerful c++ is. I'd learn just out of respect.
@ShonePizza
@ShonePizza 6 жыл бұрын
Guys, you can't use "delete" with "malloc()"
@JuvStudios
@JuvStudios 4 жыл бұрын
but you can use free()
@arturopopoli2395
@arturopopoli2395 5 жыл бұрын
thanks a lot!
@Skaarjy
@Skaarjy 4 жыл бұрын
Weird question, it's been decades, literally, since I used C. (Not C++, but good ole C). Wasn't there different ways to allocate storage besides new and malloc/calloc/realloc? I remember compact/small/tiny/..../large/huge memory models, but I vaguely remember something about allocating memory.
@xrafter
@xrafter 4 жыл бұрын
Yes, there is
@williamchamberlain2263
@williamchamberlain2263 5 жыл бұрын
Is there significant overhead in allocating memory for the stack on each new scope? I suspect that that is down to how smart the OS is in its interface and interactions with the Standard Library, but that's me being naive.
@theshermantanker7043
@theshermantanker7043 2 жыл бұрын
Thread stacks are usually native, it's usually heap allocations that need the OS's help
@ixwix
@ixwix 3 жыл бұрын
so, if I don't call delete, the memory will stay blocked until I restart my computer?
@sdn28
@sdn28 3 жыл бұрын
How to create an array of entities with different constructor (with arguments) using new?, Entity *e = new Entity[50] // is calling only the default constructor without arguments
@phantomstriker7996
@phantomstriker7996 Жыл бұрын
New is when you want to allocate a specific amount of memory in the heap
@joseluismatabuenasedano6881
@joseluismatabuenasedano6881 Жыл бұрын
what happens if you don't delete?
@fishyperil2153
@fishyperil2153 6 жыл бұрын
at 4:50, when you allocate the array of Entity objects and say that all of them aren't really stored in another adress, but rather in a row at the same adress, what are our options for acessing these Enitity objects then ? is it all done through pointer arithmetic?
@ultimatedragon4281
@ultimatedragon4281 2 жыл бұрын
for (unsigned short i = 0; i < 50; i++) { std::cout
@mba2ceo
@mba2ceo 2 жыл бұрын
How do we find the SIZE of stack ?
@AgentM124
@AgentM124 6 жыл бұрын
The New Keyword! DELETE IT
@industrialdonut7681
@industrialdonut7681 3 жыл бұрын
just had a blast allocating as much memory as I could in 0 ints !!! Got up to 11GB before windows paged the rest
@patrickg2416
@patrickg2416 3 жыл бұрын
that Ibanez shred machine >>> I see you
@pixarfilmz4769
@pixarfilmz4769 6 жыл бұрын
love
@simond3843
@simond3843 3 жыл бұрын
In the case of mass generating say mob entity's would u allocate to stack or heap?
@ultimatedragon4281
@ultimatedragon4281 2 жыл бұрын
Probably heap. Stack has a limited and small memory. Trying to put large object or large amounts of them there is probably not gonna end well. I guess in a case like this you would probably use some optimization strategy to make it more manageable for the program (like in optimized)? I am a beginner at C++ so take this guess with a grain of salt, but maybe on this case you make a bigger allocation (maybe even use a big allocation that you made beforehand) and put all of these inside that? Like a big mob's pool? I can imagine something like that being done with placement new and pointer arithmetic... That said, I am probably just saying a bunch of nonsense that anyone that actually understands about this and is reading this comment is probably going "WTF are you even talking bout?!" XD. I still think it would be on the heap though.
@codastudiode
@codastudiode 2 жыл бұрын
You saved my life!
@timothy8941
@timothy8941 Жыл бұрын
I was following some tutorials for building a game engine that called for the use of the new operator for some reason it always gave me an undefined reference to constructor error. So I tried using malloc as you described and it worked. Not sure what this means could just be my compiler but im going to continue to use malloc untill it gets me in trouble same for delete. Thank you for the video
@marianionita3282
@marianionita3282 Ай бұрын
malloc doesn't call the constructor
@alltheway99
@alltheway99 4 жыл бұрын
What about design patterns and SOLID
@kenan2386
@kenan2386 2 жыл бұрын
C++ is cool and all but have you tried blinking?
@matthewsanetra
@matthewsanetra 6 жыл бұрын
By calling new using the placement operator, could you use that to change other games memory address values? For example to change a string in another program you would call string* lang = new() string("en-US");
@TheCherno
@TheCherno 6 жыл бұрын
+Matthew Sanetra not in that way, since you program won't be able to access memory outside its process. There are however OS API functions you can call that can achieve stuff like that.
@matthewsanetra
@matthewsanetra 6 жыл бұрын
Thanks for the reply!
@williamchamberlain2263
@williamchamberlain2263 5 жыл бұрын
@@matthewsanetra my wife had a low-level code course which included homework with , _halfway_ through the instructions, a warning that "if you use the wrong memory address you might overwrite operating system instructions and have to re-install your OS". _Not_ a good university.
@ultimatedragon4281
@ultimatedragon4281 2 жыл бұрын
@@williamchamberlain2263 WTF?! LOL XD Who the hell thought that was a good idea?
@williamchamberlain2263
@williamchamberlain2263 2 жыл бұрын
@@ultimatedragon4281 someone who didn't want to set up lab equipment
@Hoppitot
@Hoppitot 2 жыл бұрын
Thx for the malloc chief, don't worry I got this
@rahulor2478
@rahulor2478 Жыл бұрын
what will happen if the program crashes before we call 'delete'
@smileynetsmileynet7922
@smileynetsmileynet7922 3 жыл бұрын
You would write in C++, because you need native code, that works on Linux AND Windows. Until my languages can write themselves in themselves, there ISN'T really a better way. As of this writing, the development version has ALMOST the same features as C, which would be the first BIG milestone.
@shariquemohammad1215
@shariquemohammad1215 3 жыл бұрын
@The Cherno ... what is the difference between new T * [n] and new T[n]?
@amrojjeh
@amrojjeh 6 жыл бұрын
int* a = new int; Would the new int *and* the a pointer be both stored in the heap or would only the new int be stored in the heap?
@TheCherno
@TheCherno 6 жыл бұрын
The pointer itself is stored on the stack, but the memory it's pointing to is on the heap.
@sachinpandhare8737
@sachinpandhare8737 5 жыл бұрын
I would like to correct something , please !!!!!! malloc(): int *a = new int; --- > will internally call the malloc() and allocate the 4 bytes on the heap and return the pointer to int, calloc(): int *a= new int[50]; ---> calls a calloc() and allocates 50 contiguous blocks of 4 byte each and returns a pointer to the first block , but it should be called a pointer to the array of integers. memory will always be allocated on heap with calloc or malloc as its dynamically allocated , one more interesting part is if you do char *strp = "TheChemo is great" ; this will also put this string on heap but a separate part of a heap called literal pool i guess (learned this 4 years before don't really remember) and this is kind of const and you can;t update this as its ---(a C programmer )
@alexdumea
@alexdumea 6 жыл бұрын
Hey Cherno, do you plan on making a video about CMake sometime?
@TheCherno
@TheCherno 6 жыл бұрын
Yes, when we start talking about cross-platform development.
@alexdumea
@alexdumea 6 жыл бұрын
TheChernoProject Awesome! Love your videos, keep it up!
@WJSWebster
@WJSWebster 6 жыл бұрын
FOR THE LOVE OF GOD PLEASE BLINK!
@youcaillou
@youcaillou 5 жыл бұрын
I have been watching all the C++ videos, from the first to this one. First things first: These videos are amazing. Coming from JavaScript, I find it super easy to learn about the concepts of C++. There is just one thing I would like to point out: You often use the word `class` when you actually mean `object`. This can be super confusing, e.g. at 1:25 in this video you say: "Easy, I'll just create a new class". This is completely different to saying "Create a new object". I am sure you are aware of this, I just want to point out that it often confuses me, when watching the videos. And with that, I shall continue watching. PS: Is there another way to donate money to you than through Patreon? I would love to make a bigger one time donation, rather than a subscription.
@Usammityduzntafraidofanythin
@Usammityduzntafraidofanythin 2 жыл бұрын
Any time creates a brand new func or class, he calls them definitions. There's a difference between Def and init/declaration
@iyaehsan3840
@iyaehsan3840 3 жыл бұрын
Ik this is completely unrelated but your eye color is so pretty XD
@user-mh6hw3ed6q
@user-mh6hw3ed6q 3 жыл бұрын
int*b= new int[50] i can use Entity* e= new(the place i wanna save) , but not used probably
@Retded
@Retded 3 жыл бұрын
Right, so Entity* e = (Entity*) malloc(sizeof(Entity)); new (e) Entity("entity"); is functionally identical?
@ayushjindal4981
@ayushjindal4981 5 жыл бұрын
Hi! Suppose we have int* p= new int[10]; delete p+3; Then is it going to free up the 4th element's memory? If no, then how can it be done? Also, if we just had: delete p; Then, is it going to free up the entire array or just the 1st element or something else?
@ayushjindal4981
@ayushjindal4981 5 жыл бұрын
@Peterolen Thanks!!
@PumpReactivationProject
@PumpReactivationProject 5 жыл бұрын
you cant really do it. Lets say you have a file or something, you load that data to an heap array and you want to delete half of it. For beginers its really obvious, you just add half of the size to the pointer and delete it, however it wont work. It wont work because the system gives you a pointer and also it associate that memory with that pointer. If it gives you a pointer X, you can not request deleting a pointer X+1, this memory address might be inside the block that you have allocated but the system does not associate any previous allocation with that, so it wont remove it. Valid tactic is to just copy that half of memory you want to keep to another block of allocated memory (this time you allocate only as much as you need to store that half) with ex. memcpy, and then you delete the original pointer. Its a good thing to store your base pointer somewhere else, like in a different variable so you always can reach and use that original one. Hope I helped, sorry for any mistakes but Im just a hobbyist :)
@mr.budders8740
@mr.budders8740 3 жыл бұрын
0:46 "Especially now in 2017"
@brentstockton5742
@brentstockton5742 4 жыл бұрын
if could "using namespace std;" that would be greaaat
@synestematic
@synestematic 2 жыл бұрын
can someone explain WHY it is so important to use the delete operator? if my program terminates without me having used it on my heap allocated pointers will that memory not be available anymore to other processes?
@ultimatedragon4281
@ultimatedragon4281 2 жыл бұрын
I wrote a big comment about that and it got deleted, so I am mad. If you are running the program with memory safety features and you aren't allocating too much memory (simple applications), the OS will free the memory when the process finishes (usually the case). if you are running the program without these safety features (simple systems that sacrifice that for performance like embedded systems or something like running linux in kernel mode), then it won't be freed even after the program ends and it can take up all your memory with time and/or multiple executions. Applications that allocate a lot of memory and/or don't control exactly how much they allocate (like if allocate inside an unbounded loop) can actually end up taking so much memory that they bring the entire system to a crawl or just get killed by the OS for taking too much memory. IF the don't delete a.k.a. free the memory after using it. Remember that a smaller application or code of today can become part of the code or a module of a bigger program tomorrow, in which case even smaller and simpler programs (when first written) can actually cause a lot of problems later (when incorporated on the bigger one) because of not freeing their memory. It is also good practice to free the memory (clean after yourselves programmers!). tl;dr : Just free your damn memory! Always!
@b78418364
@b78418364 5 жыл бұрын
So basically normal objects and variables use stack memory (which means the memory it needs can change based on the value they hold), but by declaring them with the new keyword basically we limit their memory to 4 bytes or so ? ...
@hsaidinsan6345
@hsaidinsan6345 4 жыл бұрын
Software patterns plsssssss
Implicit Conversion and the Explicit Keyword in C++
7:54
The Cherno
Рет қаралды 181 М.
How to CREATE/INSTANTIATE OBJECTS in C++
13:03
The Cherno
Рет қаралды 237 М.
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 38 МЛН
OMG😳 #tiktok #shorts #potapova_blog
00:58
Potapova_blog
Рет қаралды 4,3 МЛН
1❤️
00:17
Nonomen ノノメン
Рет қаралды 12 МЛН
THEY WANTED TO TAKE ALL HIS GOODIES 🍫🥤🍟😂
00:17
OKUNJATA
Рет қаралды 16 МЛН
Should you learn C++?? | Prime Reacts
20:29
ThePrimeTime
Рет қаралды 326 М.
Pydantic is OP, here's why
18:10
Carberra
Рет қаралды 15 М.
Mastering Memory: Allocation Techniques in C, C++, and ARM Assembly
17:05
CONST in C++
12:54
The Cherno
Рет қаралды 395 М.
The "this" keyword in C++
6:08
The Cherno
Рет қаралды 247 М.
Stack vs Heap Memory in C++
19:31
The Cherno
Рет қаралды 555 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 380 М.
Abstraction Can Make Your Code Worse
5:13
CodeAesthetic
Рет қаралды 622 М.
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 38 МЛН