pointer troubled me

Asked by shengyang

I want to port u-boot 2018.3 to my own board.I catch some thing amazing,just see my code in func dram_init_banksize

int dram_init_banksize(void)
{
 fdt_addr_t mr_base, mr_size;
 get_memory_base_size(&mr_base, &mr_size);
 /*
  * Fill in global info with description of SRAM configuration
  */
 gd->bd->bi_dram[0].start = mr_base;
 gd->bd->bi_dram[0].size = mr_size;
    fdt_addr_t * add_ptr = NULL, * add_ptr_2 = NULL;
    add_ptr = &(gd->bd->bi_dram[0].size);
    if (NULL == add_ptr)
        printf("NULL\n");
    *add_ptr = 456;
    add_ptr_2 = 0xc1e99ffC;
    *add_ptr_2 = 789;
    printf("iysheng gd->bd->bi_dram[0].size=%d add_ptr=%p *add_ptr=%d"
        " add_ptr_2=%p *add_ptr_2=%d\n", (gd->bd->bi_dram[0].size), add_ptr, *add_ptr,
        add_ptr_2, *add_ptr_2);
 return 0;
}
I get something in uart port just as below
iysheng gd->bd->bi_dram[0].size=0 add_ptr=c1e99ffc *add_ptr=0 add_ptr_2=c1e99ffc *add_ptr_2=789
how that could be happen?

Question information

Language:
English Edit question
Status:
Expired
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :
#1

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#2

Hi Shengyang,

I presume 0xc1e99ffC is device memory where write are ignored? That would explain the 0 read from gd->bd->bi_dram[0].size and *add_ptr. Regarding *addr_ptr2 2 things happen. First, the pointer is not volatile so the compiler decide to not actually read from memory and just return the last value that was stored to it. Second, the output is different from *add_ptr because it is initialized from an integer and thus the compiler does not know it points to the same object as *add_ptr. If you mark entries in the bi_dram array in gd->bd, and value pointed by add_ptr and add_ptr2 as volatile the 3 output should be consistent (and I assume 0).

Best regards,

Thoma