This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Const data changes during program execution

hi,

(lpc1317-64P)
during execution of my code const data changes. I don't know how.
I have used
const uint8_t *p[] = { "", "abcd", };

I have checked by adding the array in watch. At one point randomly data in null type gets changed to some long garbage data..

I need null character at 0 th places because I have made logic in that way.
Also I don't think how const data can get changed randomly.I have also monitired VCC during program its 3.3V. No glitch.

is this due to because I am using 36KB of 64K byte flash & 6KB ram out of 8KB available or due to optimization as compiler stores null character in RAM for faster exceution or something

As in smaller module this code works fine without any problem. Problem come when I club the module.

However const data shouldn't be changed.

  • I have checked by adding the array in watch

    There is a difference in the behaviour of the 'abstract machine' and the 'real machine' that you see in the watch window. One major reason is compiler optimizations that limit lifetime of variables. Also, the compiler may generate incomplete or incorrect debugging information. Besides, the debugger may interpret debugging information incorrectly. Of course, those would be bugs in the compiler or the debugger. Such bugs are considered non-critical since they don't affect the correctness of generated machine code.

    However const data shouldn't be changed

    I have a surprise for you: if you declare a const variable at function scope with no static keyword (that is, the variable is automatic) then the variable will be allocated on the stack. It is easy to change the contents of such variable through a pointer.

  • Various other things could also cause the change:

    Being accessed by a pointer (deliberately or otherwise) that doesn't have a 'const' qualifier;

    Stack overflow;

    Buffer over/under-run;

    DMA;

    probably others...

  • const uint8_t *p[] = { "", "abcd", };
    

    That is not quite as const as you may think it is. This is an array of two const pointers to uint8_t. Note that this means the uint8_t data themselves are not const.

    And another thing: it is easy to make the mistake of believing that 'const' qualified data are 100% totally unmodifiable, read-only data. That's not the case. 'const' only means that an otherwise correct C program will not be able to write to them. There's a difference.

  • "This is an array of two const pointers to uint8_t."

    Umm no, but this is:

    uint8_t * const p[];