Thank you! I've had to look this up many times because It's easy to confuse details between the two. This was a very concise explanation, with bite sized examples that helped make your points.
I really like these, it is actually quite difficult to find concise, beginner friendly explained concepts of fundamentals of programming which have much better leverage on getting good than "learning a language" I wonder whether how to efficiently store data, when to cache results, ..., is also considered to be "coding concepts" anyway looking forward for the next videos
This depends on the language. c/c++: variables will be on the stack unless you specify. Javascript and other garbage collected langauges: basic types: Number, Boolean, BigInt - stored on the stack arrays and objects - stored on the heap but have references on the stack. In some languages classes are stored on the heap and structs on the stack but it always depends on the language. This is all done for you in garbage collected languages.
Delete will free the heap memory that you specify. Also the reference to that heap memory will be uninitialized on the stack: int* number = new int; //creates heap memory *number = 1; //sets the value std::cout
in c++ a vector is an array that can change in size. So you can add and remove elements. For standard arrays in c/c++ you can't change the size of them. In Javascript all arrays can be resized which means they are heap allocated. It works very differently in compiled vs interpreted languages.
Yes, they are essentially the same thing just stored in a different part of ram, but the mechanics are different. Stack memory is automatically given to you and freed automatically when the program ends. Heap memory needs to be asked for and won't be freed automatically.
Both stack and heap memory are stored in RAM. They are just in stored in a different part so might not be close together. When data is accessed often, it can be put into the cache on the CPU. This is quicker than accessing data from RAM so has a performance benefit. Both stack and heap memory data can both be stored in cache memory but stack memory is usually accessed more often and more likely to be put into cache. Data is always stored in RAM and is copied into cache memory to increase performance.
@@ThomazMartinez Stack memory takes no time to create. Creating a stack variable: -Move the stack pointer down -Set the data Heap memory takes a lot more to create: Creating a heap variable: -Ask the operating system for more memory. You need to specify the size of the memory and the operating system needs to find a spare block of memory of that size. -Wait for the memory to be given to you. -Create a variable that references to the heap memory on the stack. -Set the data Generally creating the variable is the slow part. When it comes to accessing and changing the data it can depend and sometimes the difference is very small. As stack memory is closer together and accessed more often it is more likely to be stored in cache memory which increases performance.
It was a simple and understandable video, I liked it.
Thank you! I've had to look this up many times because It's easy to confuse details between the two. This was a very concise explanation, with bite sized examples that helped make your points.
I really like these, it is actually quite difficult to find concise, beginner friendly explained concepts of fundamentals of programming which have much better leverage on getting good than "learning a language"
I wonder whether how to efficiently store data, when to cache results, ..., is also considered to be "coding concepts"
anyway looking forward for the next videos
Great explanation ❤
nice vid mate, cheers
Cool video cheers
when does something go into stack or heap? maybe video?
This depends on the language.
c/c++: variables will be on the stack unless you specify.
Javascript and other garbage collected langauges:
basic types: Number, Boolean, BigInt - stored on the stack
arrays and objects - stored on the heap but have references on the stack.
In some languages classes are stored on the heap and structs on the stack but it always depends on the language.
This is all done for you in garbage collected languages.
so delete only removes from heap but not stack and for stack it does not matter?
Delete will free the heap memory that you specify. Also the reference to that heap memory will be uninitialized on the stack:
int* number = new int; //creates heap memory
*number = 1; //sets the value
std::cout
I'm comming from JS, so Vector is new thing, what is diff between vector and array?
in c++ a vector is an array that can change in size. So you can add and remove elements.
For standard arrays in c/c++ you can't change the size of them.
In Javascript all arrays can be resized which means they are heap allocated. It works very differently in compiled vs interpreted languages.
Heap and stack both stored in RAM yes?
Yes, they are essentially the same thing just stored in a different part of ram, but the mechanics are different.
Stack memory is automatically given to you and freed automatically when the program ends.
Heap memory needs to be asked for and won't be freed automatically.
So is stack then memory of CPU when they have L3 caches and so on? And heap is in RAM?
Both stack and heap memory are stored in RAM. They are just in stored in a different part so might not be close together.
When data is accessed often, it can be put into the cache on the CPU. This is quicker than accessing data from RAM so has a performance benefit. Both stack and heap memory data can both be stored in cache memory but stack memory is usually accessed more often and more likely to be put into cache.
Data is always stored in RAM and is copied into cache memory to increase performance.
@@CodingWithTom-tn7nl oh, but why then stack is faster? and good reference on web i can read about this?
@@ThomazMartinez Stack memory takes no time to create.
Creating a stack variable:
-Move the stack pointer down
-Set the data
Heap memory takes a lot more to create:
Creating a heap variable:
-Ask the operating system for more memory. You need to specify the size of the memory and the operating system needs to find a spare block of memory of that size.
-Wait for the memory to be given to you.
-Create a variable that references to the heap memory on the stack.
-Set the data
Generally creating the variable is the slow part. When it comes to accessing and changing the data it can depend and sometimes the difference is very small.
As stack memory is closer together and accessed more often it is more likely to be stored in cache memory which increases performance.
@@CodingWithTom-tn7nl wow thank you so much for all this info, looking forward for more content like this,