Undefined behaviour of const

前端 未结 4 763
灰色年华
灰色年华 2020-12-10 09:42

I never thought I will be going to ask this question but I have no idea why this happens.

const int a = 3; 
int *ptr;
ptr = (int*)( &a );
printf( \"A=%d\         


        
4条回答
  •  [愿得一人]
    2020-12-10 10:05

    The optimizer can determine that a is a constant value, and replace any reference to it with the literal 3. That explains what you see, although there's no guarantee that's what's actually happening. You'd need to study the generated assembly output for that.

提交回复
热议问题