Qbasicnews.com

Full Version: Pointer Stress Testing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Downloaded the 0.13b version of FB and have been playing around with pointers a little. Some of this is very nitpicky because most programmers are unlikely to use the expressions I am using. Also due to the lack of available documentation on FB currently, Im going mostly by C's semantics of pointers, so correct me if Im wrong on anything.

Are unary operators actually part of expressions? Im often getting: "error(9): Expected expression, found : 'something' ", when doing less than normal things with the unary * and @ operators, this is most notable here:
Code:
dim p as integer pointer, a as integer

p = @a   ' This works fine

p = @(a) ' This produces an error

When both the unary * and @ operators are used together, they should nullify the effect.
Code:
dim a as integer, b as integer, p as integer pointer

b = 10

a = *@b  ' This works correctly, a is assigned the value 10
p = @*p ' Gives error 9, with expecting expression, found *

Is pointer arithmetic meant to be commutative?
Code:
dim p as integer pointer, i as integer
dim array(0 to 5) as integer

p = @array(0)

for i = 0 to 5
  *(p + i) = i 'This works
  *(i + p) = i 'This doesn't: Error(70): Incomplete type before ')'
next

Why are you allowed to add two pointers together using pointer arithmetic?
Code:
dim p as integer pointer, i as integer
dim array(0 to 5) as integer

p = @array(0)

  *(p + p) = 10
This compiles fine, but segfaults (Annoying report your problem to Microsoft box) when run.

Do the unary @ and * operators have the same precedence? Why does this successfully compile?
Code:
dim p as integer pointer, dp as integer pointer pointer
dim a as integer

dp = &a
p = *@(@dp)

print "p = "; p ;", dp = "; dp ;"@a = "; @a
This compiles, and prints the same value for p, dp and @a. The expression: '*@(@dp)' should be equivalent to '@dp' in which case p and dp would have different values. The expression in the above code snippet shouldn't compile at all, because it doesn't make sense to take the address of an address. Note that replacing '*@(@dp)' with '*(@dp)' will give the same results (p, dp and @a all have the same value).
Another one, the following C program will print out all of the elements of the array:
Code:
#include <stdlib.h>

int main(void) {

  int i, **dp;
  int array[5];

  for(i = 0; i < 5; i++)
    array[i] = i;

  dp = malloc(sizeof(int *));
  *dp = malloc(sizeof(int));
  *dp = &array[0];

  for(i = 0; i < 5; i++)
    printf("array[%d] = %d\n", i, *(*dp + i));
}

But the FB equivalent:
Code:
dim i as integer, dp as integer pointer pointer
dim array(0 to 4) as integer

for i = 0 to 4
  array(i) = i
next

dp = allocate(len(integer pointer))
*dp = allocate(len(integer))

*dp = @array(0)

for i = 0 to 4
  print "array["; i ;"] = "; *(*dp + i)
next

Gives the expected identifier, found * error message.

Another inconsistency, and IMHO pointer division is in general a bad thing, most C compilers won't let you do it:
Code:
dim a as integer, p as integer pointer

a = p1 / 2           ' Compiles okay
a = *p1 / 2          ' Compiles okay
a = *(p1 / 2)        'Fails with: Expected ')', found: '/'
a = *(p1 + p1 / 2)   ' Compiles okay
a = *(p1 + (p1 / 2)) 'Compiles okay
Yeah, there's much to be improved yet when it comes to pointers, i'm working at the moment adding proper debugging support, but better checks and your suggestions will be added when i begin to mess with pointer type casting..

Parenthesis can't used as in C, where they are allowed pretty much everywhere, (func()), (p)->f, etc..