The NEW Keyword in C++

preview_player
Показать описание


Gear I use:
-----------------

Рекомендации по теме
Комментарии
Автор

new: hey pls give me 4 bytes
Standard-Library: no! get a job !

nazar
Автор

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

michaelwright
Автор

"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.

ssuriset
Автор

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!

chrisstone-streetlightinte
Автор

*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

leixun
Автор

I cant believe someone can actually explain things like so good. Brilliant job. Much appreciated.

muhammadammar
Автор

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.

vanDalle
Автор

_"Please give it to me"_ That's what she said.
_0:24__ _"It is actually quite deep."_ That's what she said.

MirrorsEdgeGamer
Автор

Welcome to the C++ series where "we'll talk about it in a future video"

Raymoclaus
Автор

Dude, this is my game of thrones now a days, thanks for such an amazing series :)

Djzaamir
Автор

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.

valizeth
Автор

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)

soulimanemammar
Автор

"String 28 bytes... just guessing from memory"
So, String guess* = new guess();
You guessed from your heap?

papinkelman
Автор

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.

jeeperscreeperson
Автор

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 << *a ; // supposed to be random.

But surprisingly the output will be 0!!! This is because of OS behaviour. When malloc is called, in turn OS sys_call() function is called which makes sure to provide the requested memory block to malloc, and before providing it, OS keeps all the data in memory to 0. Thats why we get 0 without even intializing the variable.

strangeruler
Автор

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

motbus
Автор

@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

WeberJ
Автор

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 to stoooreee on the heap, I was like OH MY GAWDDD, My mind has been blown!!

haykav
Автор

Love your videos, i've been learning quite a lot and using it for embeded c++ programming, keep up the great work!

pacsmile
Автор

Please do a video on linked lists and nodes! These are really important and pretty confusing

NexGenSlayer