This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to support a 32 bits pointer memory access to a non 4 byte memory address

Hi,

   I am using the C6000 CGT 7.4.4 and I found that a 32 bits pointer(for example uint32_t *a) to access an address no matter it is 4 byte alinged or not, the compiler would use LDW rather than LDNW which assumes the address is 4 byte aligned. If the content of 32 bits pointer is gotten from another type of data (8bits, 16 bits) which is not always 4 byte aligned, it might cause error.

  Is there any compiler option can disable this restriction? That being said, the pointer access always read data from its real address even it is not 4 byte aligned.

Thanks,
Adam

  • Test example and log as below:

    main.c
    ____________________________________________________________________
    uint16_t a = 0xab;
    uint16_t b = 0xbc;
    uint8_t c = 0xc;
    uint8_t d = 0xd;
    uint8_t e = 0xe;
    uint8_t f = 0xf;

    int main(void) {

    uint32_t * p_a, * p_b;
    uint32_t * p_c, * p_d, * p_e, * p_f;

    p_a = (uint32_t *)&a;
    p_b = (uint32_t *)&b;

    printf("**********************16 bits address to 32 bits pointer test!\n");

    printf("the address of 16 bits variable a is 0x%x\n", &a);

    printf("the address of 16 bits variable b is 0x%x\n", &b);

    printf("the content of 32 bits pointer p_a is 0x%x\n", p_a);

    printf("the content of 32 bits pointer p_b is 0x%x\n", p_b);

    printf("the content of *p_a is 0x%x\n", *p_a);

    printf("the content of *p_b is 0x%x\n", *p_b);

    p_c = (uint32_t *)&c;
    p_d = (uint32_t *)&d;
    p_e = (uint32_t *)&e;
    p_f = (uint32_t *)&f;

    printf("**********************8 bits address to 32 bits pointer test!\n");

    printf("the address of 8 bits variable c is 0x%x\n", &c);

    printf("the address of 8 bits variable d is 0x%x\n", &d);

    printf("the address of 8 bits variable e is 0x%x\n", &e);

    printf("the address of 8 bits variable f is 0x%x\n", &f);

    printf("the content of 32 bits pointer p_c is 0x%x\n", p_c);

    printf("the content of 32 bits pointer p_d is 0x%x\n", p_d);

    printf("the content of 32 bits pointer p_e is 0x%x\n", p_e);

    printf("the content of 32 bits pointer p_f is 0x%x\n", p_f);

    printf("the content of *p_c is 0x%x\n", *p_c);

    printf("the content of *p_d is 0x%x\n", *p_d);

    printf("the content of *p_e is 0x%x\n", *p_e);

    printf("the content of *p_f is 0x%x\n", *p_f);

    }

    ____________________________________________________________________
    Log:
    **********************16 bits address to 32 bits pointer test!
    the address of 16 bits variable a is 0x8
    the address of 16 bits variable b is 0xa
    the content of 32 bits pointer p_a is 0x8
    the content of 32 bits pointer p_b is 0xa
    the content of *p_a is 0xbc00ab
    the content of *p_b is 0xbc00ab
    **********************8 bits address to 32 bits pointer test!
    the address of 8 bits variable c is 0xc
    the address of 8 bits variable d is 0xd
    the address of 8 bits variable e is 0xe
    the address of 8 bits variable f is 0xf
    the content of 32 bits pointer p_c is 0xc
    the content of 32 bits pointer p_d is 0xd
    the content of 32 bits pointer p_e is 0xe
    the content of 32 bits pointer p_f is 0xf
    the content of *p_c is 0xf0e0d0c
    the content of *p_d is 0xf0e0d0c
    the content of *p_e is 0xf0e0d0c
    the content of *p_f is 0xf0e0d0c
  • Your code example above is a violation of the strict aliasing rule.  Long story short, the compiler presumes you will never write code like that.  For more details, please see this post.

    Thanks and regards,

    -George

  • George,
         I am clear now. Thanks a lot.

    Regards,
    Adam