一个德国面试官用skype给小弟我出的面试题

一个德国面试官用skype给我出的面试题

volitate int i=0;
void DoSomething()
{
   for(;i<10;i++)
    {  //dosomething
   }
}


加不加volitate有什么区别

我的回答是告诉编译器不要对这个变量进行优化,但优化的英文当时实在是想不起来怎么说了,所以我说tell the compiler not to change it ,悲剧了
------解决方案--------------------
tell the compiler not to change it 我感觉回答的也可以啊
------解决方案--------------------
not optimize it since it's  volatile
------解决方案--------------------
msdn says "A type qualifier that you can use to declare that an object can be modified in the program by the hardware"
------解决方案--------------------
引用:
Quote: 引用:

tell the compiler not to change it 我感觉回答的也可以啊

当时德国老哥又具体问了下,我说不是告诉程序不能改这个变量,而是告诉编译器,这老哥让我从寄存器的角度去介绍,顿时石化,就我这菜鸟英语能说明白吗,咳咳
一个德国面试官用skype给小弟我出的面试题 德国公司??
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

tell the compiler not to change it 我感觉回答的也可以啊

当时德国老哥又具体问了下,我说不是告诉程序不能改这个变量,而是告诉编译器,这老哥让我从寄存器的角度去介绍,顿时石化,就我这菜鸟英语能说明白吗,咳咳
一个德国面试官用skype给小弟我出的面试题 德国公司??

中美合资公司的德国leader,绕啊绕

急了直接开中文逆袭
------解决方案--------------------
“volatile 指出变量是随时可能发生变化的,与volatile变量有关的运算,不要进行编译优化,以免出错。” 
确实得好好学学英文...
------解决方案--------------------
一个参数既可以是const又可以是volatile吗?答案是肯定的。。。
------解决方案--------------------
你和他说
can you speak chinese?
------解决方案--------------------
这面试好有难度…………一个德国面试官用skype给小弟我出的面试题
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

Quote: 引用:

Quote: 引用:

tell the compiler not to change it 我感觉回答的也可以啊

当时德国老哥又具体问了下,我说不是告诉程序不能改这个变量,而是告诉编译器,这老哥让我从寄存器的角度去介绍,顿时石化,就我这菜鸟英语能说明白吗,咳咳
一个德国面试官用skype给小弟我出的面试题 德国公司??

中美合资公司的德国leader,绕啊绕

急了直接开中文逆袭
一个德国面试官用skype给小弟我出的面试题
------解决方案--------------------
引用:
Quote: 引用:

tell the compiler not to change it 我感觉回答的也可以啊

当时德国老哥又具体问了下,我说不是告诉程序不能改这个变量,而是告诉编译器,这老哥让我从寄存器的角度去介绍,顿时石化,就我这菜鸟英语能说明白吗,咳咳


the keyword volatile is used to ask hardware to get value of a viarant from memory, not from register.

这样回答可以不?
------解决方案--------------------
如果是单线程环境,则这个volatile没有意义。
如果是多线程环境,则有volitate时,其他线程可以读出 i 的实时变化;无volatile时,其他线程会看到 i 值一直不变。
volatile迫使编译器每次都从内存里读取 i 的值,而不是尽可能从寄存器读取。

The volatile keyword does nothing if this programme is single-threaded, but in multi-threaded situation, volatile enables other threads to get the real-time value of 'i', without it, other threads may see the 'i' never changes.

Volatile makes the compiler to always fetch the value of a variable from memory instead of registers.