Page 1 of 1

Question about constant pointer typecasts.

Posted: 24 Jun 2012 12:06
by Dany
Hi,

why does the expression

Code: Select all

Ptr := ^const TStateTransition(Machine.TransitionTable);
give an error "syntac error: expected "pointer to record (?T57)" but "pointer to array()" found,
while the expression

Code: Select all

Ptr := ^const TStateTransition(^const byte(Machine.TransitionTable));
compiles without error?

Apparently 2 subsequent type castings are needed here?

The types used are:

Code: Select all

type
     TGetEventProc = function: byte;
     
     TActionProc = procedure(Id, From, Towards, Event: byte);
     
     TStateTransition =
     record
       Fromstate, ToState, Event: byte;
       Action: ^TActionProc;
     end;
     
     TStateMachineTable = array[1] of TStateTransition; // dummy size
     
     TStateMachine =
     record
       Ident           : byte;
       TransitionTable : ^const TStateMachineTable;
       NrTransitions   : byte;
       GetEventProc    : ^TGetEventProc;
       Running         : boolean;  // running or stopped
       CurrentState    : byte;     // the current state of the state machine
       CurrentEvent    : byte;     // the current event to react upon
       NextState       : byte;     // the future state of the state machine
     end;
The usage of the above types in the expression:

Code: Select all

procedure StateMachine_Step(var Machine: TStateMachine);
var Index      : byte;
    Routine    : ^TActionProc;
    Ptr         : ^const TStateTransition;
    Found      : boolean;
begin
  ....      
      Ptr := ^const TStateTransition(^const byte(Machine.TransitionTable)); // point to first entry in the State Transition table  
  ...
end;
Thanks in advance!

Re: Question about constant pointer typecasts.

Posted: 24 Jun 2012 16:00
by JimKueneman
I want to know for the dsPIC compiler as well. What appears two pointer types are compatible. I am doing this:

TBase = record
State: byte;
Count: byte;
end;
PBase = ^TBase;

TData = record
State: byte;
Count: byte;
Data: array[0..50] of char;
end;
PData = ^TData;

..... More records expanding TBase

Now I code what type it is in the state field;

Base: PBase;
Data: PData;

if Base^.State and STATE_IS_TDATA <> 0 then
Data := PData( Base);

gives the same type of error. I have to cast it to a non pointer type then back:

if Base^.State and STATE_IS_TDATA <> 0 then
Data := PData( Word( Base));

Then all is well, sort of because now the code is assuming a 16 bit pointer for the dsPIC and is not portable between micros. I have fixed this by creating a pointer type:

{ifdef P30}Pointer = Word;{endif}
{ifdef P32}Pointer = DWord;{endif}
etc...

so now I do this:

if Base^.State and STATE_IS_TDATA <> 0 then
Data := PData( Pointer( Base));

so it "looks" like I am casting it to a generic pointer but not really.

That said I would like to suggest a generic pointer type so it is automatically based microcontroller you are compiling for and the size of the pointer.

Jim

Re: Question about constant pointer typecasts.

Posted: 27 Jun 2012 08:04
by srdjan
Dany wrote:Hi,

why does the expression

Code: Select all

Ptr := ^const TStateTransition(Machine.TransitionTable);
give an error "syntac error: expected "pointer to record (?T57)" but "pointer to array()" found,
Should be an error here, these types are not compatible and the error message is correct.
Dany wrote: while the expression

Code: Select all

Ptr := ^const TStateTransition(^const byte(Machine.TransitionTable));
compiles without error?
Byte pointer acts as an generic pointer, it can be passed to any other pointer type and vice verse with the exception of function pointers.
Therefore, by forcing this 'two step' casting, compiler makes sure that this is something you
wanted to do, not something you have done by mistake.

Re: Question about constant pointer typecasts.

Posted: 27 Jun 2012 08:10
by srdjan
JimKueneman wrote: so it "looks" like I am casting it to a generic pointer but not really.

That said I would like to suggest a generic pointer type so it is automatically based microcontroller you are compiling for and the size of the pointer.
We already have an pascal version with generic type pointers, just needs to be released :)
Unfortunately, it will take some time to properly test this one :(
For now, you can use byte pointers as generic pointers like Danny did.
This will take care of your pointer size issue mentioned here.

Re: Question about constant pointer typecasts.

Posted: 27 Jun 2012 09:03
by Dany
srdjan wrote:We already have an pascal version with generic type pointers, just needs to be released :)
Unfortunately, it will take some time to properly test this one :(
For now, you can use byte pointers as generic pointers like Danny did.
This will take care of your pointer size issue mentioned here.
Thanks!