TIC's pointer type
Pointers in TIC have some properties
of ANSI C pointers, but there's
enough difference that you should
probably read this no matter how
pointer-experienced you think you
are.
First of all, here's what a pointer
is... A "pointer" is simply an index
into a lookup-table. In that table
are "actual" pointers to the memory
blocks. The reason you want to
access memory via this table is that
the TI operating system moves data
around quite frequently, and this
table keeps track of these data
moves. The point is, if you type,
for example, x=malloc(2568);, 2568
bytes of consecutive memory will be
allocated to you (if it's available)
and the address of the beginning of
that array will be stored into the
lookup-table. Then your variable x
will contain an index into that
lookup-table. Let's say that when
the memory was allocated, there were
already 52 chunks allocated (by other
parts of your program, or by the
operating system), well then, the
pointer to the beginning of that
chunk would be in the 53rd location
in the allocation lookup-table, and
your variable x would then contain
the number 53.
Using this lookup-table and some
tricks, you can create arbitrarily-
complex data sructures... here's
how...
to allocate a certain amount of
memory and "call it"
x, type...
x=malloc(size);
This statement will allocate size
number of bytes in RAM (remember, if
you want enough space for 100 int's,
you need to allocate 400 bytes!!! --
remember, 1 int = 4 bytes), then it
will store the index number of that
block in the lookup-table into x.
To use some value in this block, just
use standard "array-accessing" syntax...
y=x[8];
A similar syntax is used for storing
to the array...
array1[10]=5;
Using all of these features, you can
easily make multi-dimentional
arrays... To do it, you can just make
an array of pointers to arrays. This
is TIC's multidimentional-array
convention, which conforms to the
ANSI-C convention. Here's how do set
it up...
/* creating a 5x12 array... */
int XDim=5;
int YDim=12;
int 2dArray;
/* setup base-level array... */
2dArray=malloc(4*XDim);
/* setup 2nd-dimension of array */
for( i=0 ; i
2dArray[i]=malloc(4*YDim);
the TIC compiler will understand the
following code...
element=2dArray[3][9];
Remember that array index counting
starts at zero!
Before leaving your program, you
should de-allocate the memory you
used, that is, "give it back" to your
TI's operating system. To do this,
use the free() function...
/* deleting a 5x12 array... */
/* delete 2nd-level memory blocks */
for( i=0 ; i
free(2dArray[i]);
/* delete base-level array... */
free(2dArray);
(For a "real-life" example of this idea,
see the Minesweeper game, mine.tic).
Of course, you could leave those
blocks in the RAM-memory, but unless
they're being pointed to by
"constant" variabls, you won't know
where they've went to, so BEWARE when
trying this. If you DO do this, it
will just "use up" extra memory.
This memory is recorded as memory in the
SYSTEM listing on your TI's
MEM menu.
This is ok if you keep track of it
for later use... Version 0.7 Beta of
TICC will have an option to "clean
up" after your program is done, that
is, to have all of your allocated
memory be automatically deleted.
If you run across an error in allocating memory
(such as not having enough memory at the time of a
call to malloc()), the program will stop
with a display screen saying that you've run out
of memory. If you want the program to do something
else (like clean up some things, or give your own
error message), you can just define a procedure, called
mallocerr(), that will be run instead
of the default error handler. Here's an example...
int Array;
int ValidMem;
proc main()
{
ValidMem=1;
Array=malloc(10);
if( ValidMem )
DoGame();
else
Fail();
}main;
proc mallocerr()
{
ValidMem=0;
}mallocerr;
...this will allow you to give the user an
out-of-memory message (the Fail()
function) if there is not enough memory
instead of just stopping with our ugly default
out-of-memory handler...
Well, that is kindof the whole story
on TIC's arrays... If you have any
questions, feel free to mail
us.