YTread Logo
YTread Logo

Pointers in C / C++ [Full Course]

May 30, 2021
it had a global variable called cache. So initially, let's say the main function is running when we play our game, the main function makes multiple calls to the play function. And what actually happens when a function calls another function is that that particular function is paused and memory is allocated for the execution of the called function. Then main will stop and playback will start running and playback will go on top of main on the stack. Now, we had a couple of local variables in play, they will all go on this stack frame, i x y, players guess y, for case one, when we had the character array C on the stack itself, so it wasn't created using a call to mallocar the character.
pointers in c c full course
Array C will also be located in this stack frame. Now, when the play function execution finishes, control will return to the main function and the memory allocated for the play function execution will be reclaimed. Each time a function call ends, the memory that is allocated on the stack is reclaimed. So there is a stack frame corresponding to each call. And as soon as that call ends, that memory is recovered. Now the main team will make another call to play because we will play several rounds. The game will then go back into the stack and be erased again when the game ends.
pointers in c c full course

More Interesting Facts About,

pointers in c c full course...

As you can see, all local variables are cleared every time the function call ends. For anything on the stack, we don't have to worry about its deallocation as it happens automatically when the function call ends. Now let's talk about the second case where a character array is created on the heap by calling the malloc function. Once again we will make multiple calls   to reproduce the function. Now what will happen this time is that we will not create the array on the stack, we will still have a variable called c, a local variable called c, but this variable will not be of type character array of size three, this variable will be of type pointer to the character and we will make a call to the malloc function to create the array on the heap and this local variable, which is a pointer to the character, will only point to this particular block of memory.
pointers in c c full course
Anything on the heap must be accessed through a pointer variable. So here we have created the array on the heap and have only kept a pointer variable on the stack. Now, when the call-to-play function completes, the memory allocated for the execution of the call-to-play function will be reclaimed. Then all   local variables will disappear. But this memory in the heap will remain unused and unreferenced and nothing in the heap will be allocated it has to be allocated explicitly by making a call to the free function or using the delete operator and think about it, we will make several calls to play   works as we We played several rounds of our game.
pointers in c c full course
And each time on each call to play we will create one of those blocks of memory in the heap that will remain unreferenced and unused when the function called to play ends. If we play 100 rounds, we will have hundreds of three-character blocks of memory and the heap unreferenced and unused. The heap does not have a fixed size. And our application can claim that it can get more memory in the heap section as long as our system is not running out of memory. And if we don't allocate this unused memory in the heap exhausting and wasting memory which is an important resource.
The memory consumption of our applications will continue to grow over time. Memory leaks are really nasty bugs in your program. Anything on the heap unused and unreferenced is garbage. In C or C++ we have to make sure, as programmers, that garbage is not created on the heap. Memory leak is nothing more than garbage growth on the heap. In languages ​​like Java and C Sharp, garbage is automatically removed from the heap, so a programmer does not have to worry about freeing or allocating memory on the heap, which is a nice feature. Avoid memory leak. In this example, we were creating an array of three characters on the heap.
What if we were creating an array of 10,000 characters and didn't free the memory after we were done using it? At the end of the function, the memory consumption would have skyrocketed like anything. Coming back to my code here, what I have done is:   I have created a character array of size 10,000 of 10,000 characters here, my logic would not change,   I will only use the first three positions in the array. I'm just trying to show you something. And at the end of this particular function, when we are done using this array on the heap, we make a call to the free function, passing it the address of this block of memory, this array, our program will work as before.
But let's run this and monitor the memory consumption one more time. Once again, I show you the task manager and I'm playing. Let's make some bets. Now be careful with the memory consumption of the dot txt game. No matter how long you play, memory consumption will skyrocket. As you can see, it's 350 6k. And it doesn't trigger even after playing for a long time. And it is not firing because we had used free to allocate the memory when we are done using it at the end of the function. Remember, we had created an array of size 10,000. And if we weren't using free then the memory would have skyrocketed like anything, the memory consumption would have skyrocketed like anything.
But because we are freeing at the end of the function, there is no increase, there is no memory leak. Finally, to summarize it, memory leak is the improper use of dynamic memory or heap memory section that causes the memory consumption of our program to increase over a period of time. Remember, memory leak always occurs due to unused and unreferenced memory blocks in the heap. Everything on the stack is automatically allocated and the stack always has a fixed size. At most we can have a stack overflow. So this was a memory leak in C c++. Thanks for watching.

If you have any copyright issue, please Contact