Memory Allocation

Memory allocation is automatically handled by the EDEN interpreter. The string and list data types are assigned by copy (i.e. each string has its own copy instead of sharing the same string through pointers as in C). For example,

S1 = "1234567890";
S2 = S1;

S2 is now the string "1234567890". If we now do

S2[1] = "X";

then S2 becomes "X234567890", but S1 is still "1234567890".

Also the EDEN interpreter allocates the exact amount of memory for the string. Reference beyond the string's or list's memory block (probably through C function calls) may have unpredictable results.

If we pass a string as an argument to an EDEN function, the string is copied, i.e. passed by value. But if we pass a string to a C function, only the address of the memory is passed, i.e. passed by reference. So, the user should be careful in calling C functions.

For instance, if we want to use the C function gets to read in a string from stdin, we must first allocate some memory to a string variable before we call gets:

S = substr("", 1, 255);
gets(S);

Here we call the function substr to assign 256 spaces (strings are terminated by an invisible NUL character) to S. Then we call gets to read a string into the memory allocated to S. By doing so, we not only allocate memory for gets to used, but also set the data type of S to the string type.


[FORWARD] [HOME] [UP] [HELP]