EmbDev.net

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


von Ma H. (Guest)


Rate this post
useful
not useful
GCC seems to have a problem dereferenzing pointers.
Example code:
1
#define    MAX_A    4
2
#define    MAX_AS  5
3
4
typedef    char    str16[16];
5
6
typedef    struct  stc_a*    ptr_stc_a;
7
8
typedef    struct  stc_a  {
9
                            U8        number_a;
10
                            str16     text_a;
11
                          } t_a, *ptr_t_a;
12
13
typedef    t_a            arr_t_a[MAX_AS];
14
typedef    arr_t_a*       ptr_arr_t_a;
15
16
17
typedef    struct  stc_b*    ptr_stc_b;
18
19
typedef    struct  stc_b    {
20
                              U8              number_b;
21
                              ptr_arr_t_a     arr_a;
22
                            } t_b, *ptr_t_b;
23
24
25
#define    PTR_ARR_A      (ptr_arr_t_a)&array_a
26
27
const      arr_t_a        array_a = {
28
                                      {0,"ZERO\0"},
29
                                      {1,"ONE\0"},
30
                                      {2,"TWO\0"},
31
                                      {3,"THREE\0"},
32
                                      {4,"FOUR\0"}
33
                                    };
34
35
          t_b            b        = { 0, PTR_ARR_A };
36
37
          ptr_t_b        ptr_b;
38
39
40
#define    CR      PUTCH0('\r')
41
42
void test_stc(void)
43
{
44
  U8    i;
45
46
  ptr_b = (ptr_t_b)&b;
47
48
  CR;
49
  my_prints("TEST\r");
50
  my_printf("ptr_b            = %lp\r",ptr_b);
51
  my_printf("&b               = %lp\r",&b);
52
  my_printf("&array_a         = %lp\r",&array_a);
53
54
  my_printf("sizeof(t_a)      = 0x%02X\r",sizeof(t_a));
55
  my_printf("sizeof(arr_t_a)  = 0x%02X\r",sizeof(arr_t_a));
56
  my_printf("sizeof(t_b)      = 0x%02X\r",sizeof(t_b));
57
58
  CR;
59
  my_prints("Addresses dereferencing ptr_b \r");
60
  my_printf("ptr_b->number_b = %lp\r",&(ptr_b->number_b));
61
  for(i=0;i<MAX_AS;i++)
62
  {
63
    my_printf("ptr_b->arr_a[%d] = %lp\r",i,&(ptr_b->arr_a[i]));
64
  }
65
66
  CR;
67
  my_prints("Addresses const array_a in FLASH \r");
68
  my_printf("&array_a = %lp\r",&(array_a));
69
  for(i=0;i<MAX_AS;i++)
70
  {
71
    my_printf("&array_a[%d] = %lp\r",i,&(array_a[i]));
72
  }
73
74
}


Debug output (UART)
1
MCU          : Spansion FM3 MB9BF516N ARM Cortex-M3
2
Firmware     : v00.02
3
CM3 CMSIS    : v04.30
4
GNUC         : v05.02.01 (46712)
5
Compiled     : Feb 29 2016 @ 13:28:05
6
7
TEST
8
ptr_b            = 0x1fff8038
9
&b               = 0x1fff8038
10
&array_a         = 0xba5c
11
sizeof(t_a)      = 0x11
12
sizeof(arr_t_a)  = 0x55
13
sizeof(t_b)      = 0x08
14
15
Addresses dereferencing ptr_b
16
ptr_b->number_b            = 0x1fff8038
17
ptr_b->arr_a[0]            = 0xba5c
18
ptr_b->arr_a[1]            = 0xbab1
19
ptr_b->arr_a[2]            = 0xbb06
20
ptr_b->arr_a[3]            = 0xbb5b
21
ptr_b->arr_a[4]            = 0xbbb0
22
23
Addresses const array_a in FLASH
24
&array_a                   = 0xba5c
25
&array_a[0]                = 0xba5c
26
&array_a[1]                = 0xba6d
27
&array_a[2]                = 0xba7e
28
&array_a[3]                = 0xba8f
29
&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
sizeof(t_a)...

Ideas/Workarounds???

: Locked by Moderator
von Sebastian V. (sebi_s)


Rate this post
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:
1
my_printf("ptr_b->arr_a[%d] = %p\n", i, &((*ptr_b->arr_a)[i]));
or
1
my_printf("ptr_b->arr_a[%d] = %lp\r",i,&(ptr_b->arr_a[0][i]));

BTW: You code is really awful.

von Jörg W. (dl8dtl) (Moderator)


Rate this post
useful
not useful

This topic is locked and can not be replied to.