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.

Simple C code question



Hello again all,

This is probably a really simple C code problem, but at this given moment in time I cannot firgure out where my mistake is.

Below is  example code that demonstrates the issue.

const unsigned long a = 0x12345678;
const unsigned long b = (unsigned long)((unsigned long)(&a) + (unsigned long)(0x1000));

Why is the compiler throwing the following warning:

 warning: pointer points outside of underlying object

Thanks,

Paul

  • The compiler is being a little "too clever" here.  It's trying to save you from a situation that isn't strictly legal C by warning you about this construct, which is legal C but with implementation-defined behavior.  It is illegal to dereference a pointer that has been modified to point outside the object it originally pointed at (in this case, the object is "a").  However, your code doesn't do that, so it's technically legal.  I don't know of a way to write the code so that you don't get the warning.  The only thing I can suggest is to suppress the warning.  (Unneeded casts and parentheses removed for clarity).

    #pragma diag_suppress 172
    const unsigned long a = 0x12345678;
    const unsigned long b = (unsigned long)&a + 0x1000;
    #pragma diag_default 172
    
  • I got the same warning with very similar code, I don't like adding pramas, or keep it at minimum.

     

    I solved it by changing my code this way (some decleratiosn hidden for clarity)

    from:

    b = (unsigned long)&a + 0x1000;

    To: 

    b = (unsigned long)&a
    b += 0x1000;

    Then the compiler does not confuse the addition with the now gone poointer context.