[SOLVED] Prevent const variables optimization

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • [SOLVED] Prevent const variables optimization

    Hi,

    I want to prevent the compiler from optimizing for const variables.

    Is there an attribute that must be specified when declaring a const variable
    to instruct the compiler in this regard?

    Best regards,

    Neculai
  • Hi Neculai,

    I assume the compiler is removing the variables?
    In that case try volatile.
    Does that work?

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hi,

    This is an example that shows that the optimization done by the compiler on the "const" variables is not necessary.

    // serial_number is placed in the flash section at FLASH_APPL_CONFIG_ADDR

    const uint32_t serial_number __attribute__((section(".appl_cfg"))) = 1;


    flash_page_erase(FLASH_APPL_CONFIG_ADDR);

    NRF_LOG_INFO("Serial number: %x", serial_number);

    The value printed is "1" and is incorrect due to the optimization made by the compiler (the compiler does not have to do such an optimization or this optimization should be optional !!)

    The correct value to be printed is 0xFFFFFFFF!

    Is there an attribute that must be specified when declaring "const" variables so that the compiler does not perform such optimization?

    Thank you in advance for your answer!

    Neculai
  • Hi Neculai,

    the compiler behaves correctly. The compiler is simply allowed to do that for const variables like you are using them. For the compiler const tells him this is a variable that will never change. So the compiler may place and set this value anywhere and treat it similarly like a define.
    The compiler does not know that you will do a flash erase at some point and the value will still correctly be replaced to 1 instead of the erase value.

    Instead you need to access the variable via e.g. a pointer:

    Source Code

    1. volatile unsigned int* p;
    2. p = (volatile unsigned int*)&serial_number;
    3. printf("Serial number: %x", *p);
    That way you force the compiler to actually place the const at the section you want and make sure an actual memory read is done to access the serial number.
    For safe measure we recommend setting project option "keep symbols" and set serial_number there.

    Best regards,
    Nino
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hi Neculai,
    do you use the segger linker ?
    For the segger linker i have a simple solution in the linker script

    keep { section .vectors,
    section .appl_cfg };

    In your c file you have to add the attribute used:

    const uint32_t serial_number __attribute__((used, section(".appl_cfg"))) = 1;


    I suggest to use the segger linker because, there are many other cool features ...