pointer behaviour

Discuss about beta versions of mikroPascal compiler.
Post Reply
Author
Message
jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

pointer behaviour

#1 Post by jpc » 02 Jun 2006 22:02

While testing some complex structures i found some strange behaviour possibly related to alignment issue's , take a close look at this in the debugger :

Code: Select all

program pointer1;
type str20 = string[20];
type tmyrec2 = record
                 a,b,d : byte;
               end;
type tmyrec1 = record
                 positionx ,
                 positiony ,
                 positionz : longint;
                 settings : array[3] of tmyrec2;
               end;




var myrec      : ^tmyrec1;
    therealvar : tmyrec1;
    res        : longint;
    tmp2       : ^tmyrec2;

    jpc : str20;

begin
   therealvar.positionx := 1723;          // ok
   myrec := @therealvar;                   // ok
   myrec^.positiony:= 321;                // ok
   therealvar.settings[2].b := 123;      //  does not show correct in debugger 
  
   jpc[12]:=therealvar.settings[2].b;   // however was assigned correct as it show here
   tmp2 := @therealvar.settings[0];    // pointer itself seems now to be located inside therealvar !
   tmp2^.b := 17;                             // ok
end.

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: pointer behaviour

#2 Post by zristic » 02 Jun 2006 23:10

Can't check now, but what happens when you declare the fourth byte inside tmyrec2?

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#3 Post by jpc » 03 Jun 2006 07:49

probably did point in the wrong direction , adding this fourth bye does not solve the issue , i noticed however that the pointer myrec is created at the address of therealvar.settings[1] , as if the space required by this variable is not correctly managed . I have the impression that the compiler was not aware tmyrec1.settings is an array[] of tmyrec2 but instead behaves as if it was just a single tmyrec2-type as last field. This problem actually shows twice in the example , after the pointer myrec beeing allocated at the wrong address , the string jpc does the same starting just after myrec.

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

#4 Post by jpc » 08 Dec 2008 14:34

as it is in the same direction i continue with the following :
while running some tests to compare mP8.3 beta / mP Pro AVR beta and mP dsPIC with this code-fragment i found some bad behaviour , the snippet compiles ok ( mP PIC we are talking about here ) but it looks like there is something wrong in calculating the offset. I am hoping to get the WITH statement in the future in order to deal with similar situations.

Code: Select all

program test;
// demonstrates problem with indirect access by pointer to complex variables


const spi_buffsize    : word =100;
type spi_channel_type  = record
                           head,
                           tail,
                           termflag,
                           match     : byte;
                           empty,
                           full      : boolean;
                           delimiter : string[5];
                           rx_buff,
                           tx_buff   : array[0..spi_buffsize] of byte;
                         end;

const
      max_slave       = 3;

var spi_channels : array[0..max_slave] of spi_channel_type;
    this_channel : ^spi_channel_type;




begin
 this_channel := @spi_channels[1];
 this_channel^.head := 8;
 this_channel^.tx_buff[5] := 'x';
 this_channel^.tx_buff[this_channel^.head] := 'x';    // this changes this_channel^.delimiter[1] !!!!!!!
                                                      // seems to be error in calculating the offset
end.
the dsPIC compiler has additional problem here as it seems to generate illegal asm

Post Reply

Return to “mikroPascal Beta testing”