Why do I prefer "Stack" than "Heap" in coding?


I. The beginning
Figure 1.0

As a student who just start to learn how the computer runs after we write those lines of code, I recently know the definition of " Stack" and "Heap" in memory process. At that time, I was quite confused about which one I would choose then after searching and learning on the internet, I finally got my answer that I would pick "Stack" rather than "Heap". The reasons are bellow here.

 II. The middle

At the first start, let's get to know what is "Stack" and "Heap" meaning? And why we have to know them?

1. The definition

What is "Stack" and "Heap" meaning?

So basically, coding is just placing instructions (operators like +,-,.. or for, while,...) from one register to another one. So the first task we have to do is initializing variables and there are two ways to do this which are "Stack" and "Heap". Now we know that the basic working of  "Stack" and "Heap" is just the style we create the variables in the memory.

Why we have to know them?

In my view, when you programming, your job is not just writing codes to fulfill the requirements, because some of your codes may affect the memory (like memory leak), the time running the program is too long as you allocate a large number of variables or you may use up all the memory (even though the size of the current memory is enormous but you can consider it as a case). Thus, you have to make sure that your memory is not damaged and the way you choose to allocate the variable whether "Stack" or "Heap" is very important because memory is costly !!!

Now let's dive into the working and the difference between "Stack" and "Heap".

2. Stack

Basically, when you initialize the variables like this:

int v = 5;
string sth = "allocation";

You are creating new values in "Stack" way. Imagine that memory is the pipe and when you use Stack, the program will automatically drop the first variable in the pipe and the second one will put later on. In other words, the address of the first and second variables are next to each others. Depend on the size of the type of the variable, each of them will have the suitable size of storage. And the time to find the suitable place for the variable is unexpectedly short. Moreover, the variables will automatically be deleted when they run out of the scope.

Figure 2.0

See if you try to cout the x, you cannot do that because the variable has already been deleted, but you can cout the y as it is still in the scope.

However, there are some obstacles when dealing with Stack. For example, you want to fetch the variable inside a particular function to the place where it was called, or you just want to keep that value for later use, etc.

In conclusion, if you are the one who care about the time consuming for running the program and do not want to use those variables, which were created, again, "Stack" allocation is the best way. The upcoming section is about "Heap", off we go!!
 
3. Heap
up coming 👺
























  

Comments