7

I am learning about operating systems and the thing that I do not understand exactly are heaps and stacks. I know the benefits and how each works, but in the case of dynamic languages I can not figure out how is the stack allocated.

In static typed languages all primitive data types are stored on the stack since they are small and will be deallocated more or less in the same order they were allocated, however in languages like PHP this is not known until the run time. So how is the stack size and variable allocation possible?

If I understand correctly stack size is determined on compile time by analysing number of primitive data types and some offset. How is the process done in PHP or other dynamic languages?

If this question is kick in the dark, please give me some guides how to learn about this

trincot
  • 317,000
  • 35
  • 244
  • 286
gorgi93
  • 2,457
  • 8
  • 30
  • 53

2 Answers2

2
  1. If I understand it correctly, all PHP data types are zval. And zval is basing on a few "Z" data type (defined in C). There are limit number of "real" data type. I believe they are stored in the stack.

    So although users can create new data type, but they are not "real" data type but different zval values. And the number and definition of "real" data type are stable. Thus the size and content of stack won't change in run time.

  2. The size of memory is limited. PHP have to actively do reference counting and garbage collection. For more detail, please read this slide about PHP memory management.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
0

In the case of a dynamic programming language, the intepreter takes care of that. An interpret act is roughly like a computer. Let's assume a C-like interpreted programming language running on 32-bit machine:

c = 10;

Basically the following happens to every expression, in a minimal interpret:

  • the interpret implicitly deduce c variable type to as an integer;
  • c identifier is put on symbol table
  • right side of expression is evaluated and the result is associated to c identifier.

And when you write an expression like this:

a = c * 2

A look up in the symbol table is performed, looking for the c identifier and if found, this entry from the symbol table hold reference e.g, where our 10 value was stored in the memory. This 10 value is loaded and then "replaced" in the expression.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
The Mask
  • 17,007
  • 37
  • 111
  • 185
  • how about the values that are not unassigned? – gorgi93 Jun 14 '14 at 21:05
  • 1
    They still live on memory. – The Mask Jun 14 '14 at 21:34
  • as what kind or type of variable? – gorgi93 Jun 16 '14 at 15:53
  • As type which the last value is. If it's variable 'a' or a number constant like 10 the type will be identifier or number respectively. You're maybe confusing that the expression only gets parsed if '=' symbol is seen. – The Mask Jun 16 '14 at 17:57
  • 1
    It doesn't really matter. Any value on expression gets pushed on memory and in the case of the assignment symbol is seen its computed result is put at this location. Otherwise the value unused and no variable points to it. For example, you're parsing a language like C of void expression type and poping value from an operand stack as long you generate code to it, one left value will be always on the operand stack. It could be poped manually. – The Mask Jun 16 '14 at 18:00