Author Topic: Is Indirection Possible?  (Read 4109 times)

0 Members and 1 Guest are viewing this topic.

Mike Lobanovsky

  • Guest
Is Indirection Possible?
« on: January 14, 2014, 12:31:05 PM »
Is indirection with * possible in OxygenBasic? If yes then what's the proper syntax for an O2 equivalent to C's *(p+1)?

Peter

  • Guest
Re: Is Indirection Possible?
« Reply #1 on: January 14, 2014, 01:09:42 PM »
Hi,

if I understand correctly then:

Code: [Select]
int a = 5   'a = variable
int p = &a  'p = pointer

Print *p
print *p+1

print *(p+1) 'is address of a+1
print &a     'is address of a 

Mike Lobanovsky

  • Guest
Re: Is Indirection Possible?
« Reply #2 on: January 14, 2014, 02:17:19 PM »
Hi and thanks Peter,

But no, your *(p+1) should, in its simplest implementation, return the number stored at address p+1 rather than what the sum of p+1 equals to.

The O2 help doesn't specify the exact meaning of operators such as * (apart from usual multiplication), or & (apart from usual bitwise AND), or @ at all, etc.

So this is what I've found out so far by trial and error:

Code: [Select]
int a[1]
a[0]=5
a[1]=6
print a[0]
print a[1]

int p = @a[0]
print p ' surprisingly address
p = &a[0]
print p ' and here too

print *p ' value at p=5; correct
print *(p+4) ' value at p+4 doesn't work; 6 expected

Am I correct in saying that even if * indirection is possible, pointer arithmetics as such isn't supported at all?
« Last Edit: January 14, 2014, 03:00:23 PM by Mike Lobanovsky »

Aurel

  • Guest
Re: Is Indirection Possible?
« Reply #3 on: January 14, 2014, 10:48:17 PM »
Indirection in Oxygen Basic work at many levels...
here is your example which work..
Code: [Select]
indexbase 0
int a[1]
a[0]=5   'zero index
a[1]=6
print a[0]
print a[1]

sys p,res ' -> sys is a pointer not INT
p = @a[0]
print "adress:" p ' surprisingly address
res = *(p+4) '-> inc pointer to 4 bytes and set pointer res,right?

print "value at P:" +  *p ' value at p=5; correct
print "value at (p+4):" + *res ' value at p+4 doesn't work; 6 expected -> work!
« Last Edit: January 14, 2014, 10:57:42 PM by Aurel »

Aurel

  • Guest
Re: Is Indirection Possible?
« Reply #4 on: January 15, 2014, 12:04:53 AM »
and another (same) example with addition:
Code: [Select]
indexbase 0
int a[1]
a[0]=5
a[1]=6
print a[0]
print a[1]

int res
sys p,p2
p = @a[0]
print "adress:" p ' surprisingly address
p2 = *(p+4)
res = *p + *p2

print "value at P:" +  *p ' value at p=5; correct
print "value at (p+4):" + *p2 ' value at p+4 doesn't work; 6 expected -> work!

print "sum:" + res

Mike Lobanovsky

  • Guest
Re: Is Indirection Possible?
« Reply #5 on: January 15, 2014, 12:18:28 AM »
There are two basic misconceptions in your code, Aurel.

Firstly, sys is not necessarily a pointer. In fact, Oxygen's sys, long and int are currently three different aliases to (i.e. names of) one and the same data type, a 32-bit integer. In 32-bit C, this data type also has three aliases - long, long int, or simply int. 32-bit addresses are expressed as integers so here you may use any alias you like.

Charles is promoting sys because he plans to extend Oxygen to 64 bits where the meaning of long and int may differ. Oxygen's sys will remain bitness-independent so if you get accustomed to using sys in your own code then this code will stay valid in both x86 and x64 environments without any modification.

Secondly,

res = *(p+4)
................
print "value at (p+4):" + *res ' work!

is not a solution. It "work!" but only in your hack based on the Oxygen parser's failure to intercept a syntax error at *(p+4) which yields such an unexpected result. As I said, if *p works then *(p+4) should return the value stored 4 bytes away from the address p.

In more advanced languages, the compiler also keeps track of how long the data that the pointer refers to is. So if the pointer refers to a mere integer like in our case, then it will regard *(p+1) as "p plus 4 bytes" because the pointer p points to a[...] which is a 32-bit (4-byte) integer. This is called pointer arithmetics.

And finally, your hack doesn't really need so much effort. It is equivalent to just

p +=4
print "value at (p+4):" + *p ' workS!


Thanks for trying anyway, Aurel, and hope my response helps you sort out what is what. :)

Aurel

  • Guest
Re: Is Indirection Possible?
« Reply #6 on: January 15, 2014, 12:53:47 AM »
Quote
Firstly, sys is not necessarily a pointer. In fact, Oxygen's sys, long and int are currently three different aliases to (i.e. names of) one and the same data type, a 32-bit integer. In 32-bit C, this data type also has three aliases - long, long int, or simply int. 32-bit addresses are expressed as integers so here you may use any alias you like.
First of all ,i know that.
Second ,you say that is not possible,right?
So everything is ok!

By the way you must know that print is a function derived from api MesageBox which accept
strptr,right?

Mike Lobanovsky

  • Guest
Re: Is Indirection Possible?
« Reply #7 on: January 15, 2014, 01:28:24 AM »
Aurel,

I gather you didn't get my message. You see, I am not here to polemicize with you for nothing. While posting in my threads, please restrict yourself exclusively to statements which you are 100% sure of and which you are prepared to give prooflinks to, while being brief and most importantly, constructive.

I said I appreciated your effort and I am also using the word "please" three times in this message alone. I am expecting you to call me by my given name when addressing me and use the words "please" and "thank you" in civil quantities. If you are not prepared to do that, please put me on your ignore list and continue playing cat and mouse with somebody else as if I am not registered on this forum at all. You already know that your attempts at mocking me may be extremely dangerous to your health and catastrophic to your public reputation, if any.

Thank you.

Aurel

  • Guest
Re: Is Indirection Possible?
« Reply #8 on: January 15, 2014, 01:57:32 AM »
Quote
You already know that your attempts at mocking me may be extremely dangerous to your health and catastrophic to your public reputation, if any

First of all i don't have nothing against you.
extremly dangerus... my answer is I DON'T CARE.
public reputation ...really ...I DON'T CARE.

Hey Mike...
And why you are so angry , i just show that is possible ...nothing else.
I really don't understand why you are so sensitive? ::)

PS...Please stop teaching me what is what.

JRS

  • Guest
Re: Is Indirection Possible?
« Reply #9 on: January 15, 2014, 02:15:47 AM »
Hey Mike, it's only mid month. Even though Aurel seems to have toned it down a bit, I still think we need to follow through till at least the end of the month. He has his own forums with multiple log-ins if he needs to keep his English skills from getting rusty.  :D

Mike Lobanovsky

  • Guest
Re: Is Indirection Possible?
« Reply #10 on: January 15, 2014, 02:38:30 AM »
Haha John,

Guess what? You really don't have to learn anything at all. Just scribble down three short phrases like "Awesome!", "You're my hero!" and "You made my day!" to peep into from time to time and your forum may pass for super-dooper international all right.




Mike Lobanovsky

  • Guest
Re: Is Indirection Possible?
« Reply #11 on: January 15, 2014, 02:45:46 AM »
Charles,

Can you kindly explain if the behavioral differences of *p and *(p+1) were a design decision or just a glitch in the language implementation? My initial question was in fact addressed primarily to you even if somewhat transparently...

Thanks in advance,

Charles Pegge

  • Guest
Re: Is Indirection Possible?
« Reply #12 on: January 15, 2014, 03:03:57 AM »
There was a recent sneeky bug which escaped detection and I fixed it on the 6th of January, relating to C-style indirection expressions:

The C-style indirection expression is currently restricted to system integers:

Code: [Select]
sys v[4]={10,20,30,40}
p=@v
print *p '10
print *(p+4) '20
print v[2]   '20

The usual Oxygen way is to map a variable/array to an address.

Code: [Select]
single v[4]={10,20,30,40}
single p at (@v)
print p[2] '20

'using p as a simple pointer variable:
'
'step to the third p

@p+=2*sizeof single

print p
« Last Edit: January 15, 2014, 03:12:09 AM by Charles Pegge »

Peter

  • Guest
Re: Is Indirection Possible?
« Reply #13 on: January 15, 2014, 04:09:40 AM »
Hi Charles,

Code: [Select]
sys v[4]={10,20,30,40}
p=@v
print *p '10
print *(p+4) '20   
print v[2]   '20
print *(p+4) '20  gives me an address (30474756)  

Charles Pegge

  • Guest
Re: Is Indirection Possible?
« Reply #14 on: January 15, 2014, 04:52:07 AM »
Yes, that is the bug, now try the corrected Oxygen.dll:

http://www.oxygenbasic.org/forum/index.php?topic=749