pointers question

BlitzMax Forums/BlitzMax Programming/pointers question

djdee(Posted 2006) [#1]
Hey everybody :)

In school we have recently been learning about abstract datatypes and sorting-algorithms, as well as problem-trees and other stuff. We did this in python. I noticed how lovely it is to work with pointers, is there a ways to work with pointers in blitzmax. I have investigated the Ptr but it seems you always have to deciede what type it is anyways, like for example:

Local x:int Ptr

So with that said, whats the difference? Since blitzmax normal variables are "references" to its objects, i cant really see the difference. (as i understand, the only difference is that the garbagecollector doesnt take care of leftovers automaticly)
Is there a ways to just specify a pointer, and let it point to whatever you want ?


Gabriel(Posted 2006) [#2]
BlitzMax normal variables are not "references" to objects. The standard datatypes are not objects at all. Just strings and arrays.

For the second part of your question, Byte Ptr's are general purpose, do-anything pointers.


Dreamora(Posted 2006) [#3]
To clearly specify Gabriels explanation:

Numeric types are all by value. (speed reason)

All other data types are objects and thus by reference (by typesafe object references, not C like pointer)


djdee(Posted 2006) [#4]
Aha i see, big thanx for that. It seems they cant be pointed directly to data, such as 2.4567866 or "Hello world", but to objects only.


djdee(Posted 2006) [#5]
That raises a new question then.

Lets say i have a Queue. It consists of two pointers, First, and Last, Who points to a chain of Nodes (Each Node points to the next in the chain). When you add a new node it is added last in the chain, and when you pick one out, you take the first. When you want to clear this Queue, can you just set the first and last pointers to null and the chain in between is lost, or do you have to manually go through the entiry chain of nodes setting all the pointers to null ?


djdee(Posted 2006) [#6]
Im asking out of the garbagecollector/memoryleak perspective...


Dreamora(Posted 2006) [#7]
If your nodes only have a next and no prev reference, then null-ifying first and last references will clear the list, IF your values don't hold a value to the nodes.

This means:

Type Queue has reference to first:node, last:node

Node has reference next:node, value:object

the value objects don't have any reference to either of both.


djdee(Posted 2006) [#8]
Ok i think i get it, as long as the nodes point to the objects stored in the queue, but not the other way around.

Also i should have mentioned it, but im here assuming i use a byte Ptr as a value pointer in my nodes to let them be able to take whatever i want to store in the queue.

So to clarify your answer, by nullifying my first and last pointers in the Queue class, the garbagecollector will collect all nodes that was in the Queue and anything that the nodes was pointing to. Assuming nothing else is pointing to the objects or nodes, and assuming the objects in the nodes doesnt point back to the queue in any way.


SculptureOfSoul(Posted 2006) [#9]
All types share type object as their parent. Or maybe I should say all types are of type object. Unless you need to store any of the standard datatypes, you can simply make the value of the node an object, and then cast it back when retrieving/using it.

If you do need to store values that are comprised of only a standard data type, you can either wrap them in your own custom type and then store them, or you could use byte pointers.


djdee(Posted 2006) [#10]
Aha thats useful to... Just looked up Object, didnt know that one existed.