Posted: Wed May 14, 2003 4:11 pm Post subject: Using pointer to access a varible
I believe in C++ its called dereferencing (not sure tho)
I know how to do this stuff in c++ but dont know and cant find the turing syntax (does it exist?)
I need to take a pointer to a variable then use it to change the value of the variable
Also does anyone know how to take a variable then create a pointer to the variable?
Any help would be appreciated....
Sponsor Sponsor
Asok
Posted: Wed May 14, 2003 4:20 pm Post subject: (No subject)
I've used it in c++ many times but I don't really see the use for turing, since it's linear there is always a workaround instead of making a pointer.
Catalyst
Posted: Wed May 14, 2003 4:23 pm Post subject: (No subject)
ive got class for handling events (its for an rts)
in it ive got an array of pointers to real vars
the class need to be able to access these variables on its own to execucte the action in the event (since the event will be dormant in a queue at times)
Im sure i could get around this somehow but this is the way that would make it simplest for me
Dan
Posted: Wed May 14, 2003 5:15 pm Post subject: (No subject)
you could probley just make them golabe.
i tried to figer it out but so far it looks like this could not be done in turing
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Homer_simpson
Posted: Wed May 14, 2003 5:28 pm Post subject: (No subject)
i think this only works for integers....
code:
addr address of a variable
Syntax addr (reference)
Description The addr attribute is used to find the integer address of a variable or non scalar constant. This is implementation-dependent. This address may be used in an indirection operation @.
Example Set a to be the address of x.
var x : real
var a : addressint := addr (x)
Details The value of the address produced by addr is of type addressint, an integer type whose range is that of the underlying memory addresses.
The concept of an address is implementation-dependent. For example, an optimizing compiler could determine that a variable does not require space because the program could be computed without the variable with no change in output. However, in most implementations, types have a predictable size and variables of that type occupy that number of bytes in memory.
Catalyst
Posted: Wed May 14, 2003 6:56 pm Post subject: (No subject)
i cant make the vars global since the ones it uses are in a class
the addr() thing ive looked at, it might work if i can work in some type cheating
Catalyst
Posted: Thu May 15, 2003 5:02 pm Post subject: (No subject)
addr doesnt work
it crashes turing when it is used with its operator (@)
i think the might be a way to do it with bind
Homer_simpson
Posted: Thu May 15, 2003 5:07 pm Post subject: (No subject)
how bout
code:
Syntax addressint
Description The addressint (address integer) type is an integer type whose range of value is the same as that of the underlying computer. This range is, by its nature, implementation-dependent. On 32-bit architectures, it is commonly the same range as nat4 (4-byte natural number).
Example Record r contains three fields, one of which has type char(28). Variable a is an integer whose range of values is the same as the addresses of the underlying computer. This assigns B to the seventh character of a record of type r which is assumed to be located at absolute address a.
type r :
record
i : int
c28 : char (28)
c11 : char (11)
end record
var a : addressint % An integer
"¦ % a is assigned an integer value
r @ (a) . c28 (7) := 'B' % Use indirection operator @
Details Although addressint is called an integer type, it is commonly equivalent to a natural type such as nat4 (for 32-bit machines).
Be careful not to confuse addressint with pointer types. In low level languages such as assembler and C, addresses and pointers are the same. In Turing, however, a pointer is a high level concept that is more abstract than a machine address. A Turing pointer is a reference to an object, and the representation of this reference depends upon the implementation. In current Turing implementations, pointers (which are by default checked) are represented as a time stamp (a unique number) together with an address. The time stamp is used to make sure that the pointer actually locates an object. There are also unchecked pointers. An unchecked pointer's internal representation is a machine address. You can use type cheats (a dangerous feature) to translate between addressint and unchecked pointers. This is meaningful in current implementations.
Sponsor Sponsor
Homer_simpson
Posted: Thu May 15, 2003 5:10 pm Post subject: (No subject)
aaaaaaahhhh.... i think this is what u need....
Note the pointer in the code
code:
collection declaration
Syntax A collectionDeclaration is one of:
(a) var id { , id } : collection of typeSpec
(b) var id { , id } : collection of forward typeId
Description A collection declaration creates a new collection (or collections). A collection can be thought of as an array whose elements are dynamically created (by new) and deleted (by free). Elements of a collection are referred to by the collection's name subscripted by a pointer. See also new, free and pointer.
Example Create a collection that will represent a binary tree.
var tree : collection of
record
name : string (10)
left, right : pointer to tree
end record
var root : pointer to tree
new tree, root
tree (root) . name := "Adam"
Details The statement "new C,p" creates a new element in collection C and sets p to point at i. If there is no more memory space for the element, though, p is set to nil (C), which is the null pointer for collection C. The statement "free C,p" deletes the element of C pointed to by p and sets p to nil (C). In each case, p is passed as a var parameter and must be a variable of the pointer type of C.
The keyword forward (form b above) is used to specify that the typeId of the collection elements will be given later in the collection's scope. The later declaration must appear at the same level (in the same list of declarations and statements) as the original declaration. This allows cyclic collections, for example, when a collection contains pointers to another collection, which in turn contains pointers to the first collection. In this case, the typeId is the name of the type that has not yet been declared; typeId cannot be used until its declaration appears. A collection whose element type is forward can be used only to declare pointers to it until the type's declaration is given.
Suppose pointer q is equal to pointer p and the element they point to is deleted by "free C,p". We say q is a dangling pointer because it seems to locate an element, but the element no longer exists. A dangling pointer is considered to be an uninitialized value. It cannot be assigned, compared, used as a collection subscript, or passed to free.
Collections cannot be assigned, compared, passed as parameters, bound to, or named by a const declaration. Collections must not be declared in procedures, functions, records or unions.
The same short forms for classes can be also used for collections. These include omission of the collection name in new, free and nil together with the ^ and -> notations. Pointers to types (see pointer) can also be used, which are often more convenient to use than collections.
The syntax of a collectionDeclaration presented above has been simplified by leaving out unchecked collections. With this feature, a collectionDeclaration is one of:
(a) var id { , id } : [ unchecked ] collection of typeSpec
(b) var id { , id } : [ unchecked ] collection of forward typeId
When unchecked is specified, the checking to verify that pointers actually locate elements is removed. This checking is done using a "time stamp" attached to each element and pointer, and making sure that these match with each other. When unchecked is specified, the execution is dangerous, but faster and smaller, and the pointers become simply machine addresses (as in C).
and here are some examples....
code:
Description A variable declared as a pointer type is used to locate an element of a collection or class or a value of a type. The new statement creates a new element (or value) and places the element's location in a pointer variable. The free statement destroys an element located by a pointer variable.
Example Using a collection, declare a list or records and allocate one record.
var list : collection of
record
contents : string ( 10 )
next : pointer to list
end record
var first : pointer to list
new list, first
Example Create a collection that will represent a binary tree.
var tree : collection of
record
name : string ( 10 )
left, right : pointer to tree
end record
var root : pointer to tree
new tree, root
tree ( root ).name := "Adam"
Example Using a class, create an object of that class. The object is located by the start pointer. The name field of the object is set to Ed.
class node
export var next, var name
name : string ( 25 )
next : pointer to node % Short form: next : ^ node
end node
var start : pointer to node % Short form: var start : ^ node
new node, start % Short form: new start
node ( start ) . name := "Ed" % Short form: start->name:="Ed"
Catalyst
Posted: Thu May 15, 2003 5:15 pm Post subject: (No subject)
pointers can only locate classes and collections in turing, and dont do all the things i need them to do
I did find a solution, addr() does work The reason it crashed me was b/c i used it flexible arrays (which apprently screws up the mem addresses)
Addr() can be used just like c++ (c) pointers
get the address of is like so:
code:
var x:int:=10
var address:addressint:=addr(x)
then u can dereference it with the @ operator
code:
int@(address):=12
put x
void
Posted: Thu May 15, 2003 5:21 pm Post subject: (No subject)
whoa!!!....i never knew you could dereference in turing....i always thought it was a basic level program lang....hmm....very interesting....now...if turing wasnt so goddamn slow at compiling and running...and didnt lag so much..then you could actually make something cool in there
Homer_simpson
Posted: Thu May 15, 2003 5:49 pm Post subject: (No subject)
the code doesn't crash on my comp and it works as it's supposed to...
Catalyst
Posted: Thu May 15, 2003 6:32 pm Post subject: (No subject)
it crashed on mine earlier because i was using flexible arrays
that was code showing how to use it after i realized why i was crashing
nate
Posted: Thu May 15, 2003 6:42 pm Post subject: homer has time
Homer you have a lot of time on your hands to write all that code, do they not give you homework? Lol w.e
At least your trying
-Nate
Catalyst
Posted: Thu May 15, 2003 7:17 pm Post subject: (No subject)