Forum: ARM programming with GCC/GNU tools GCC ARM Embedded -> Compiler bug! Dereferenzing of Pointers.

Author: Ma H. (Guest)
Posted on:

Rate this post
0 useful
not useful
GCC seems to have a problem dereferenzing pointers.
Example code:
#define    MAX_A    4
#define    MAX_AS  5

typedef    char    str16[16];

typedef    struct  stc_a*    ptr_stc_a;

typedef    struct  stc_a  {
                            U8        number_a;
                            str16     text_a;
                          } t_a, *ptr_t_a;

typedef    t_a            arr_t_a[MAX_AS];
typedef    arr_t_a*       ptr_arr_t_a;

typedef    struct  stc_b*    ptr_stc_b;

typedef    struct  stc_b    {
                              U8              number_b;
                              ptr_arr_t_a     arr_a;
                            } t_b, *ptr_t_b;

#define    PTR_ARR_A      (ptr_arr_t_a)&array_a

const      arr_t_a        array_a = {

          t_b            b        = { 0, PTR_ARR_A };

          ptr_t_b        ptr_b;

#define    CR      PUTCH0('\r')

void test_stc(void)
  U8    i;

  ptr_b = (ptr_t_b)&b;

  my_printf("ptr_b            = %lp\r",ptr_b);
  my_printf("&b               = %lp\r",&b);
  my_printf("&array_a         = %lp\r",&array_a);

  my_printf("sizeof(t_a)      = 0x%02X\r",sizeof(t_a));
  my_printf("sizeof(arr_t_a)  = 0x%02X\r",sizeof(arr_t_a));
  my_printf("sizeof(t_b)      = 0x%02X\r",sizeof(t_b));

  my_prints("Addresses dereferencing ptr_b \r");
  my_printf("ptr_b->number_b = %lp\r",&(ptr_b->number_b));
    my_printf("ptr_b->arr_a[%d] = %lp\r",i,&(ptr_b->arr_a[i]));

  my_prints("Addresses const array_a in FLASH \r");
  my_printf("&array_a = %lp\r",&(array_a));
    my_printf("&array_a[%d] = %lp\r",i,&(array_a[i]));


Debug output (UART)
MCU          : Spansion FM3 MB9BF516N ARM Cortex-M3
Firmware     : v00.02
CM3 CMSIS    : v04.30
GNUC         : v05.02.01 (46712)
Compiled     : Feb 29 2016 @ 13:28:05

ptr_b            = 0x1fff8038
&b               = 0x1fff8038
&array_a         = 0xba5c
sizeof(t_a)      = 0x11
sizeof(arr_t_a)  = 0x55
sizeof(t_b)      = 0x08

Addresses dereferencing ptr_b
ptr_b->number_b            = 0x1fff8038
ptr_b->arr_a[0]            = 0xba5c
ptr_b->arr_a[1]            = 0xbab1
ptr_b->arr_a[2]            = 0xbb06
ptr_b->arr_a[3]            = 0xbb5b
ptr_b->arr_a[4]            = 0xbbb0

Addresses const array_a in FLASH
&array_a                   = 0xba5c
&array_a[0]                = 0xba5c
&array_a[1]                = 0xba6d
&array_a[2]                = 0xba7e
&array_a[3]                = 0xba8f
&array_a[4]                = 0xbaa0

As you can see accessing the array via "ptr_b->arr_a[x]"
the pointer is incremented with sizeof(arr_t_a) instead


: Locked by Moderator
Author: Sebastian V. O. (sebi_s)
Posted on:

Rate this post
0 useful
not useful
This is not a bug in GCC but in your source code. If you create a 
pointer to an array you get exactly that. You either want a pointer to 
the first element of the array or you have to access it like this:
my_printf("ptr_b->arr_a[%d] = %p\n", i, &((*ptr_b->arr_a)[i]));
my_printf("ptr_b->arr_a[%d] = %lp\r",i,&(ptr_b->arr_a[0][i]));

BTW: You code is really awful.

Author: Jörg Wunsch (dl8dtl) (Moderator)
Posted on:

Rate this post
0 useful
not useful

This topic is locked and can not be replied to.