02.03.2014 Views

Tornado

Tornado

Tornado

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Tornado</strong> 2.0<br />

User’s Guide<br />

After extended development sessions in <strong>Tornado</strong> shells, the cumulative memory<br />

used for strings may be noticeable. If this is a problem, restart your target server.<br />

5.3.9 Ambiguity of Arrays and Pointers<br />

In a C expression, a nonsubscripted reference to an array has a special meaning,<br />

namely the address of the first element of the array. The shell, to be compatible,<br />

should use the address obtained from the symbol table as the value of such a<br />

reference, rather than the contents of memory at that address. Unfortunately, the<br />

information that the identifier is an array, like all data type information, is not<br />

available after compilation. For example, if a module contains the following:<br />

char string [ ] = "hello";<br />

you might be tempted to enter a shell expression like:<br />

➊<br />

-> printf (string)<br />

While this would be correct in C, the shell will pass the first 4 bytes of the string<br />

itself to printf( ), instead of the address of the string. To correct this, the shell<br />

expression must explicitly take the address of the identifier:<br />

➋<br />

-> printf (&string)<br />

To make matters worse, in C if the identifier had been declared a character pointer<br />

instead of a character array:<br />

char *string = "hello";<br />

then to a compiler ➊ would be correct and ➋ would be wrong! This is especially<br />

confusing since C allows pointers to be subscripted exactly like arrays, so that the<br />

value of string[0] would be “h” in either of the above declarations.<br />

The moral of the story is that array references and pointer references in shell<br />

expressions are different from their C counterparts. In particular, array references<br />

require an explicit application of the address operator &.<br />

5.3.10 Pointer Arithmetic<br />

While the C language treats pointer arithmetic specially, the shell C interpreter<br />

does not, because it treats all non-type-cast variables as 4-byte integers.<br />

184

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!