Today I found real teacher who can make me love c programming to go deep dive in this field thank you sir for making such beautiful lecture series for us graduate student who got stuck in such hard concepts which was explained by you in easy manner. Huge Respect & love from India
Global variables are accessible even from other c modules by using the extern statement. However, if u add static to the global variable, then it is not accessible from other modules. This means that you can define another global static variable with the same name in another module, and it will be separate from the first one which was declared in the other module.
I use static as well not all the time just as you pointed out it’s actually an important part to know so one knows when it’s needed or not and also used for functions I think he made a big mistake as beginners looking at this video are going to think it’s optional and not necessary at all.
I've been learning this on the university for couple of years, and none made me understood this that good as you. Really quality content. Thank you and keep pushing!
This is a really good explanation of the variable lifetimes. I came here because programming language analysis class just introduced this, and the textbook did not have good explanations on these concepts.
@Jonathan Muckell @Anyone So does that mean when you push the first thing onto the stack. It always occupies the highest memory address first? So it allocates at the very top?
The memory regions is are used across programming languages. It's a computing fundamental that is supported by both the operating system and the underlying hardware architecture. Many programming languages try to hide some of the messy details from the programmer (C gives you a lot more control). For example, in Java when you use the keyword "new" you are allocating memory space on the heap. The programmer never needs to free this memory, since Java uses a concept called Garbage Collection that automatically frees memory that will no longer be accessed.
@@jonathanmuckell9827 not exactly what I meant to ask. I meant "is such memory layout/arrangement (stack at the "beginning" of RAM, heap near the "end", and they grow towards each other; static located "outside" of range that memory range between heap and stack) is a standard way of memory representation for all programming languages (regardless is it have manual (C, C++) or automatic (C#, Java) memory management) or such approach specific only to C? For example, if I write char * heap_char_1 = new char; char * heap_char_2 = new char; char * heap_char_3 = new char; char stack_char = "a"; in C++, on computer that have only 100 bytes of RAM, and then print addresses of all four characters into console heap_char_1 would have address 100, heap_char_2 99, heap_char_3 98 and stack_char 0, or something like that?! Or that would be true only in C?
@@Manuel-j3q OK - Good question. It is language independent, since it is enforced at the hardware-level. For example, here is an example of the MIPS architecture. If you look on the second page, you can see the documentation for the MIPS processor's memory allocation. Stack grows from a high address down, while the heap grows from a lower address up. This is common practice across various processor architectures. courses.cs.washington.edu/courses/cse378/09au/MIPS_Green_Sheet.pdf
@@jonathanmuckell9827 huh, I did not expected that it enforced by hardware. Thank you for clarification. One last question, which is outside of video topic I think. What if I have two programs, HelloWorld and FooBar, running at the same time, probably on different threads, on a single computer. How OS would manage memory for them in this case? Divide memory in half between this two programs? Or place both stacks/heap/static near each others? Or that too specific case that have its own specific rules?
@@Manuel-j3q That's another really good question. There are a couple different parts to the question. The first part relates to whether you have separate threads or processes. Threads share a common memory address space. Two threads tend to have 2 different stacks, but will can share other variables in heap or static memory regions. Processes have completely separate addresses spaces and will have independent stacks, heaps, static memory regions. The OS uses a concept called Virtual Addresses to divide the memory space between different processes. More information on that topic can be found here: en.wikipedia.org/wiki/Virtual_address_space
Hi Jonathan, thank you for the video. It's very didactic. I have a question: can you access a location in the heap memory (previously allocated with malloc or using "new" in C++) from any function in any class different from the one where the allocation was made? If so, what is the syntax. Thank you
Today I found real teacher who can make me love c programming to go deep dive in this field
thank you sir for making such beautiful lecture series for us graduate student who got stuck in such hard concepts which was explained by you in easy manner.
Huge Respect & love from India
Thank you very much, it it difficult to find real c programming tutorials, and u provided a perfect one. Please do not stop
Thanks for the encouragement! Glad you found this helpful.
This is the best video online explaining heap and stack.
Studying CS in Germany and by far you are the best. NO ONE can explain it like you. Shame on my Prof
Global variables are accessible even from other c modules by using the extern statement. However, if u add static to the global variable, then it is not accessible from other modules. This means that you can define another global static variable with the same name in another module, and it will be separate from the first one which was declared in the other module.
I use static as well not all the time just as you pointed out it’s actually an important part to know so one knows when it’s needed or not and also used for functions I think he made a big mistake as beginners looking at this video are going to think it’s optional and not necessary at all.
Your videos about programming are a treasure. thanks a lot!
I've been learning this on the university for couple of years, and none made me understood this that good as you. Really quality content. Thank you and keep pushing!
Finally its crystal clear now for me this topic
Amazing explanation on this memory topic. Well done Sir, well done!
I wished I had watched this video when I started c coding! Damn job explaining pointers!
Great explanation and super useful. Thanks from the UK.
This is a really good explanation of the variable lifetimes.
I came here because programming language analysis class just introduced this, and the textbook did not have good explanations on these concepts.
You're so good at explaining concepts!
I appreciate this series. You are a great teacher.
Jonathan Muckell you are a wonderful teacher
great job thank you so much
please continue
A very good informative session. Keep it up..
Glad you liked the video!
Wonderfully explained; thanks a lot Prof. Jonathan Muckell
Great teaching!
Great video for reviewing these concepts. Correct and concise.
Awesome video, Jonathan. Your slow, detailed explanations really help with drilling the points into my slow brain.
Best explanation! And I like the visual aids.
You're truly a good teacher. 👍
Great video! Well explained
Nice video with good explanation thanks bro for this
Glad it was helpful!
Perfectly explained.
@Jonathan Muckell @Anyone
So does that mean when you push the first thing onto the stack. It always occupies the highest memory address first? So it allocates at the very top?
the stack vs static memory example at 10:00 really made something click for me
Great video with great examples!!! Thank you very much.
nice examples!!
What a great teacher!
excellent course ! keep up the good work
Thank you very much teacher.
Thanks for sharing this very clear, helpful video!
Fantastic explanations, thank you professor!
Does such system of memory regions applies to all languages (C family, Python, Java, etc.) or only to C specifically?
The memory regions is are used across programming languages. It's a computing fundamental that is supported by both the operating system and the underlying hardware architecture. Many programming languages try to hide some of the messy details from the programmer (C gives you a lot more control). For example, in Java when you use the keyword "new" you are allocating memory space on the heap. The programmer never needs to free this memory, since Java uses a concept called Garbage Collection that automatically frees memory that will no longer be accessed.
@@jonathanmuckell9827 not exactly what I meant to ask.
I meant "is such memory layout/arrangement (stack at the "beginning" of RAM, heap near the "end", and they grow towards each other; static located "outside" of range that memory range between heap and stack) is a standard way of memory representation for all programming languages (regardless is it have manual (C, C++) or automatic (C#, Java) memory management) or such approach specific only to C?
For example, if I write
char * heap_char_1 = new char;
char * heap_char_2 = new char;
char * heap_char_3 = new char;
char stack_char = "a";
in C++, on computer that have only 100 bytes of RAM, and then print addresses of all four characters into console heap_char_1 would have address 100, heap_char_2 99, heap_char_3 98 and stack_char 0, or something like that?! Or that would be true only in C?
@@Manuel-j3q OK - Good question. It is language independent, since it is enforced at the hardware-level. For example, here is an example of the MIPS architecture. If you look on the second page, you can see the documentation for the MIPS processor's memory allocation. Stack grows from a high address down, while the heap grows from a lower address up. This is common practice across various processor architectures. courses.cs.washington.edu/courses/cse378/09au/MIPS_Green_Sheet.pdf
@@jonathanmuckell9827 huh, I did not expected that it enforced by hardware. Thank you for clarification.
One last question, which is outside of video topic I think.
What if I have two programs, HelloWorld and FooBar, running at the same time, probably on different threads, on a single computer. How OS would manage memory for them in this case?
Divide memory in half between this two programs?
Or place both stacks/heap/static near each others?
Or that too specific case that have its own specific rules?
@@Manuel-j3q That's another really good question. There are a couple different parts to the question. The first part relates to whether you have separate threads or processes. Threads share a common memory address space. Two threads tend to have 2 different stacks, but will can share other variables in heap or static memory regions. Processes have completely separate addresses spaces and will have independent stacks, heaps, static memory regions. The OS uses a concept called Virtual Addresses to divide the memory space between different processes. More information on that topic can be found here: en.wikipedia.org/wiki/Virtual_address_space
Yoooooooooooooooooooo, that makes so much sense now.
Thank U sir, that was helpful
May i know what is the purpose of storing address in a pointer when anyways the variable is not removed in heap memory?
thank you very much, really helpful. You are gold.
Since HEAP has unlimited memory space available, is the HEAP inside the process memory region or outside ?
8:00 doesnt static in c mean that its a private variable/function for that file only?
23:00 sizeof is an operator
Hi Jonathan, thank you for the video. It's very didactic. I have a question: can you access a location in the heap memory (previously allocated with malloc or using "new" in C++) from any function in any class different from the one where the allocation was made? If so, what is the syntax. Thank you
But can't you update the value of a static variable?I thought it was impossible to change the value of a static variable
Wait im confused. Does malloc return a void pointer ?
Yes
Thank u, you are amazing !!!!!!!
sizeof is an operator not a function
sizeof is not a function, it's an operator
Yes, you are correct. Thanks for the clarification. Additional details can be found here: www.geeksforgeeks.org/sizeof-operator-c/
hello
Explaining this without mentioning the activation record is a waste of time.
Use rust lol
very boring presentation
then you are in the wrong place ! you could just don't watch the video, this man is a legend !!