next up previous contents
Next: Colectarea reziduurilor Up: Date Previous: Valori memorii   Cuprins

Asignari

Exista doua tipuri de asignari: prin valoare si prin referinta. Asignarea prin referinta e sub forma: `variable' = `expression'; Asignarea prin valoare arata astfel: =`variable' = `expression';

Structurile de date ale variabilelor arata asa3.9:

  Var1                    Var2
+------+                +------+
|   *---.		|  *   |
+------+ \		+--|---+
          \               /
           \   +-----+   /
1           `->|  2  |<-'
               +-----+
               |  *  |
               +--|--+
                  |
                  |
                  v
               +-----+
2              |  2  |
               +-----+
               | INT |
               +-----+
               | 100 |
               +-----+
                  ^
                  |
               +--|--+
               |  *  |
               +-----+
               |  1  |
               +--|--+
                  ^
  Var3            |
+------+          |
|   *-------------'
+------+

               +-----+
3              |  1  |
               +-----+
               | PNT |
               +-----+
               |[0,0]|
               +-----+
                  ^
                  |
               +--|--+
               |  *  |
               +-----+
4              |  1  |
               +--|--+
                  ^
  Var4            |
+------+          |
|   *-------------'
+------+

Var1 == Var2 == Var3 == 100
Var4 == [0,0]

Daca facem `Var2 = Var4', 2-ul din linia 1 se va schimba in 1, iar 1-ul din linia 4 se va incrementa. Daca mai departe facem `Var3 = Var5', the 1-ul din linia 1 se va schimba in 0, decrementandu-se astfel 2-ul din linia 2. Iata ce se obtine:

Var1 == 100
Var2 == Var4 == [0, 0]

               +-----+
3              |  1  |
               +-----+
               | PNT |
               +-----+
               |[0,0]|
               +-----+
                  ^
                  |
               +--|--+
               |  *  |
               +-----+
4              |  2  |<----------.
               +--|--+           |
                  ^              |
  Var4            |              |
+------+	  |              |
|   *-------------'              |
+------+                         |
                                 |
  Var1                    Var2   |
+------S		+------S |
|   *---.		|  *-----'
+------S \		+------S
          \
           \   +-----+
1           `->|  1  |
               +-----+
               |  *  |
               +--|--+
                  |
                  |
                  v
               +-----+
2              |  1  |
               +-----+
               | INT |
               +-----+
               | 100 |
               +-----+
Totusi, daca am fi incecput prin `=Var2 = Var4', 2-ul din linia 1 nu se va schimba, 2-ul din linia 2 se va decrementa si 1-ul din linia 3 se va incrementa:
Var2 == Var1 == Var4 == [0,0]
Var3 == 100

  Var1                    Var2
+------S		+------+
|   *---.		|  *   |
+------S \		+--|---+
          \               /
           \   +-----+   /
1           `->|  2  |<-'
               +-----+
               |  *  |
               +--|--+
                  |
                  `--------.
                           |
               +-----+     |
2              |  1  |     |
               +-----+     |
               | INT |     |
               +-----+     |
               | 100 |     |
               +-----+     |
                  ^        |
                  |        |
               +--|--+     |
               |  *  |     |
               +-----+     |
               |  1  |     |
               +--|--+     |
                  ^        |
  Var3            |        |
+------S	  |        |
|   *-------------'        |
+------S                   |
                           |
               +-----+     |
3              |  2  |     |
               +-----+     |
               | PNT |<----'
               +-----+
               |[0,0]|
               +-----+
                  ^
                  |
               +--|--+
               |  *  |
               +-----+
4              |  1  |
               +--|--+
                  ^
  Var4            |
+------S	  |
|   *-------------'
+------S


Sebastian Glita 2002-06-19