Author Topic: Pointer syntax...  (Read 7823 times)

0 Members and 2 Guests are viewing this topic.

Charles Pegge

  • Guest
Re: Pointer syntax...
« Reply #15 on: June 12, 2011, 01:23:42 PM »
You have to be careful with string pointering because they are memory managed by the system but you can do it like this:

Code: OxygenBasic
  1. string * ps
  2. string s
  3.  
  4. s="It's a string"
  5.  
  6. @ps = @s
  7.  
  8. print ps
  9.  
  10.  

Whenever 's' changes you will see it in ps. You can use this principle for any type: integer float string ..

Charles
« Last Edit: June 12, 2011, 01:31:09 PM by Charles Pegge »

Aurel

  • Guest
Re: Pointer syntax...
« Reply #16 on: June 14, 2011, 02:31:17 AM »
Charles if i understand properly what pointer is -> integer which hold adress
of variable - is that right?
or im completely wrong ?

Charles Pegge

  • Guest
Re: Pointer syntax...
« Reply #17 on: June 14, 2011, 03:54:09 AM »

Hi Aurel,

You can use any 4 byte integer to be hold the address of another variable.

But to make this value useful the compiler needs to know what type it is intended to be.

To extend the above example:

Code: OxygenBasic
  1.  
  2. sys p 'can hold any address
  3. string s
  4. string *ps 'string byref / string pointer
  5.  
  6. s="Hello!"
  7. p=@s
  8. @ps=p
  9.  
  10. print ps 'Hello!'
  11. print p 'string address
  12.  
  13.  

Charles

Aurel

  • Guest
Re: Pointer syntax...
« Reply #18 on: June 14, 2011, 04:14:25 AM »
Thanks Charles ...so im right ;D
I see now how things work,thanks again on explanation... ;)

kryton9

  • Guest
Re: Pointer syntax...
« Reply #19 on: June 14, 2012, 04:30:54 PM »
Confused again, as both of these work, probably because Oxygen is so smart. Which is correct however?

Code: OxygenBasic
  1. method Get() as cVec2 ptr
  2.       cVec2 temp
  3.       temp.x = x
  4.       temp.y = y
  5.       return temp
  6. end method

Code: OxygenBasic
  1. method Get() as cVec2 ptr
  2.       cVec2 temp
  3.       temp.x = x
  4.       temp.y = y
  5.       return @temp
  6. end method

Charles Pegge

  • Guest
Re: Pointer syntax...
« Reply #20 on: June 14, 2012, 09:58:00 PM »
Hi Kent,

Yes both of these return a vec2 pointer. Oxygen figures out that it cannot return classes or higher types directly, only pointers.

But you have to be careful. Your temp is a local variable and therefore goes out of scope once the function ends. The solution is to create a dynamic temp.

method Get() as cVec2  
      new cvec2 temp
      temp.x = x  
      temp.y = y  
      return temp  
end method


With one further piece of syntax, you can use this method as a class factory.

the word let is for creating tynamic variables and objects, initialised with a pointer:

let w=v.copy

Here is an example, of an object cloning itself. We have made all the pointers invisible :)

Code: OxygenBasic
  1. class cvec2
  2.  
  3. float x,y
  4.  
  5. method constructor()
  6. end method
  7.  
  8. method destructor()
  9. end method
  10.  
  11. method Copy() as cVec2  
  12.       new cvec2 temp
  13.       temp.x = x  
  14.       temp.y = y  
  15.       return temp  
  16. end method
  17.  
  18. end class
  19.  
  20. new cvec2 v
  21. v<=1.5,2.25
  22.  
  23. let w=v.copy
  24.  
  25. print w.x "," w.y
  26.  
  27. del v
  28. del w
  29.  

Charles
« Last Edit: June 14, 2012, 10:06:44 PM by Charles Pegge »

kryton9

  • Guest
Re: Pointer syntax...
« Reply #21 on: June 14, 2012, 10:11:55 PM »
You are confusing me Charles. Too much too absorb. Cool, but I need to learn a step at a time :(

1. First, which of the ones that I have is correct without oxygen magic?
2. Why does it work now if temp is a local variable as it is now?
3. "Your temp is a local variable and therefore goes out of scope once the function ends. " But many times local variables are used in a method and returned. This is confusing.
4. In your example when is temp deleted?

Charles Pegge

  • Guest
Re: Pointer syntax...
« Reply #22 on: June 14, 2012, 10:56:21 PM »
Both of your examples work but they are unsafe, since the memory they point to is disallocated stack space.

You can safely return local primitives by-value but you cannot safely return any local variables by-reference, that is to say, by pointer.

When local integers and floats go out of scope, they are not specifically deleted, more abandoned and left to perish in reusable stack space. Local strings suffer a similar fate. They live in heap space, which is specifically disallocated by the garbage collector at the end of the procedure. But when a local string is returned, it's name is removed from the garbage collector's 'death' list, and it is spared the fate of the other local strings.

In my example, temp lives in heap space, and is not on the garbage collector's list. So it remains there until you delete it.

Charles


kryton9

  • Guest
Re: Pointer syntax...
« Reply #23 on: June 14, 2012, 11:02:50 PM »
Thanks for the info Charles. All of this helps as I come back to the forums and reread posts to refresh my memory.