Computer Science Canada

"free" function for simple array.

Author:  MysticVegeta [ Mon Nov 07, 2005 7:54 pm ]
Post subject:  "free" function for simple array.

i am aware of the "free" function used to "free"/"empty" a flexible array, i was wondering if we could do the same with regular array.

Author:  beard0 [ Mon Nov 07, 2005 9:56 pm ]
Post subject:  Re: "free" function for simple array.

MysticVegeta wrote:
i am aware of the "free" function used to "free"/"empty" a flexible array, i was wondering if we could do the same with regular array.


Why not just try, and see if it works?

Author:  MysticVegeta [ Mon Nov 07, 2005 11:29 pm ]
Post subject: 

I did:
Quote:
Array grid is not flexible and hence cannot be 'free'ed

Author:  beard0 [ Tue Nov 08, 2005 12:36 am ]
Post subject: 

Then why did you ask?

I could understand "Why can't..."
In which case the answer would be because free varName is a shorthand for
code:
new varName, 0

which only works with flexible arrays, as they are the only ones whose size is flexible.

Author:  do_pete [ Tue Nov 08, 2005 1:49 pm ]
Post subject: 

If you want to make all the things in a variable equal 0 you can use xomething like this:
code:
var variableName : array 1 .. 5, 1 .. 5 of int

for x : 1 .. 5
    for y : 1 .. 5
        variableName (x, y) := 0
    end for
end for

Author:  MysticVegeta [ Tue Nov 08, 2005 2:03 pm ]
Post subject: 

No, I want them equal to nothing, nothing so that grid(x, y) returns an error of "No value for variable". Just like we can do in flexible arrays. I thought it could be done, guess i was wrong.

Author:  do_pete [ Tue Nov 08, 2005 2:49 pm ]
Post subject: 

when you declare you array then there will be no value for variable

Author:  Tony [ Tue Nov 08, 2005 3:06 pm ]
Post subject: 

turing doesn't appear to allow you to drop a non-dynamically allocated variable.

The best way would be to include the array in a type, and use a pointer to that type. You could free a pointer afterwards.

Or if you're feeling adventurous, try the following out

Turing:

var arr : array 1 .. 2 of int := init (1, 2)
var test : array 1 .. 2 of int
var buffer : array 1 .. (256 div 4) of int %reserve memory for our tricks
put "array : ", arr (1), " ", arr (2)
var address : addressint := addr (arr)
put "located at: ", address
test (1) := int@(address)
test (2) := int@(address + sizeof (int))
put "test array: ", test (1), " ", test (2)
string@(address) := "Tony was here"
put "the original array has became: ", arr (1), " ", arr (2)
put "a copy has remain in test: ", test (1), " ", test (2), " located at: ", addr (test)
put "since the difference between arr and test is: ", addr (test) - addr (arr)
put "which is less than size of the string put in place: ", sizeof (string)
put "this explains why test array was overwriten as well"

Author:  beard0 [ Tue Nov 08, 2005 4:10 pm ]
Post subject: 

Tony wrote:
Or if you're feeling adventurous, try the following out


Fun, I hadn't really had the chance to try this out before. I'll have to play with this.

Author:  MysticVegeta [ Tue Nov 08, 2005 7:13 pm ]
Post subject: 

do_pete wrote:
when you declare you array then there will be no value for variable


I meant after changing its value.


: