Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Changing Data Types and Undeclaring Variables.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zampano




PostPosted: Fri Nov 23, 2007 11:10 am   Post subject: Changing Data Types and Undeclaring Variables.

Is there any way to undeclare or redeclare the type of a variable? For example, how can change the size of a particular array or change it (permanently) to a string, discarding all the data it held.
Sponsor
Sponsor
Sponsor
sponsor
Mazer




PostPosted: Fri Nov 23, 2007 12:46 pm   Post subject: RE:Changing Data Types and Undeclaring Variables.

You can change the size of the array but as far as changing the type of a variable, would you really want to? Maybe you could provide me with an example of where you want to do something like that, because it just screams bad idea to me.
Tony




PostPosted: Fri Nov 23, 2007 2:44 pm   Post subject: RE:Changing Data Types and Undeclaring Variables.

you could change the size of an array with flexible arrays. Changing the variable type though? That does seem like a bad design.

If you really want to though, I suppose you could create your own type that would hold two variables - string and an int flag. The flag will specify the type of data that is encoded into string. The problem is that you'd always have to access this through your own encoder/decoder functions. Seems like a lot of needless overhead.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Zampano




PostPosted: Fri Nov 23, 2007 5:17 pm   Post subject: Re: Changing Data Types and Undeclaring Variables.

Well, the reason I saw sense in changing variable types or the size of arrays (thanks for the solution) is that I only need certain once or twice in a program and it would be inefficient to let them lie there idly, taking up memory.
I therefore sought to undeclare them, but as you guys haven't said anything about that I guess it is impossible, I thought I should probably reuse the same variable so that the space wouldn't go to waste. At times I would have to change the type to do this.

I realized earlier that it was probably a stupid idea, but changing a type is only an alternative to freeing information from a variable, which I want to do not because it's a bad idea, but becuase I want to make a program as efficient as it possibly can be.

Apropos, is there any chance that sometime in the future a tutorial could be made which addresses methods used to make a program efficient? Whenever I make something in Turing, I run into dilemnas where I think "Should I create a new variable or should I just use the one I already have and then change that variable later?". At times like these I don't know which will be more costly to the efficiency of the program. A tutorial of the nature described would be enormously helpful to me.
Saad




PostPosted: Fri Nov 23, 2007 5:27 pm   Post subject: RE:Changing Data Types and Undeclaring Variables.

If you really want memory management you can use pointers.
eg

var foo : ^int
new foo
^foo := 1
put ^foo
free foo

Or
var foo : ^int
var moo : int
cheat (addressint, foo) := addr (moo)
moo := 2
put ^foo
Tony




PostPosted: Fri Nov 23, 2007 7:39 pm   Post subject: Re: Changing Data Types and Undeclaring Variables.

Saad is right. If you think you can do a better job at managing memory than the garbage collector (I don't suppose Turing comes with one?) you do that with pointers.

Zampano @ Fri Nov 23, 2007 5:17 pm wrote:
I thought I should probably reuse the same variable so that the space wouldn't go to waste.

If you have a variable called num_sheep and you start using it to store someone's name instead... I think the created confusion will not be worth the few bytes of memory you save. Besides, change of datatype is, almost, out of the question. You should read up on how computer memory actually works (hardware, OS level, etc).

Really simple example to illustrate the point -- strings take up more space than integers do. So if you turn a location reserved for an integer into a string, the extra length will spill over to arbitrary (well actually it's simply whatever is next) memory location -- which could very well be another variable.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: