I'm sorry, but the example is a little obscure for me. Let me tell you instead what I know about the different segments (in a very brief manner of course):
When a program is running, different parts of it goes to different segments (places) in memory. This is so because although a program might seem a monolithic thing at first sight, without the possibility of dividing it for any good reason, this isn't so. The first division is fairly simple:
Code & Data
Code is what the program is supposed to do, and Data is what the program uses to store stuff (and eventually read it). So for example, if you have:
a = b + c;
In the Code segment you'll have something like this:
> take value of [b]
> take value of [c]
> sum those values
> store the result in [a]
And in the Data segment you'll have something like this:
> [a] is 8
> [b] is 3
> [c] is 5
This way the code part is composed of one instruction after another, and read the data from somewhere else in memory.
But not all data goes to the Data segment, some of it goes to the Stack segment... Why?
The answer is functions. Functions are reusable pieces of code that take parameters to work with. Not only that, functions also have it's own set of variables, arrays, structures. All of them also go to the Stack segment. But why?
Suppose you have a recursive function that calls itself (I'm not going to provide an example, suffice to say you have a recursive function to calculate the factorial of a number). Every time the function calls itself their parameters differ (and also their internal variables). So you have for a same piece of code, several collections of values that have to be maintained in memory in order for the program to work. Those values 'stack' on top of each other. Every time you call a function, the values of that function 'stacks' on top of the function that called it. Similarly, every time a function returns, the values of that function are popped out of the stack.
So basically:
- The Data segment contains values that are global to the program, or static values that are always maintained.
- The Stack segment contains values that are part of a particular function in a particular time of the life of the program (if you call that function again, those values might differ).
Ok, but why the Heap segment then? The answer is that for some values, its size is unknown at compile time (the program must be running and its size may vary from one run to another). If a function has one of this values there is no way they can stack them in the Stack segment, because they might increase in size and corrupt the data above it. Values that can change during run time (aka dynamic) must go somewhere else in memory so as not to compromise the process.
Why don't all variables go to the Heap segment then? That's a good question, but believe me, there are very good reasons for a program to be divided in a Code, a Data, a Stack and a Heap segment.
I hope to have been clear in my explanation. Regards.