volatile int vfoo = 0;
void func()
{
int bar;
do
{
bar = vfoo; // L.7
}while(bar!=1);
return;
}
Run Code Online (Sandbox Code Playgroud)
此代码忙等待变量变为1。如果第一遍vfoo未设置为1,我会卡在里面吗。
这段代码编译时没有警告。标准对此有何看法?
vfoo被声明为volatile. 因此,读这个变量应该不进行优化。volatile资格。是否允许编译器优化写入bar?.ie 编译器将对 进行读取访问vfoo,并允许丢弃此值而不将其分配给bar(在 L.7)。我问过这个问题.我现在的问题是这是如何工作的?详细说明,我如何指向尚未初始化的对象.我已经制作了这个MWE并且它显示该对象是创建的副本而不是复制分配.该对象尚未初始化但我能够指向它.
#include <iostream>
class Foo {
public:
int x;
Foo(const Foo& ori_foo) {
std::cout << "constructor" << std::endl;
x = ori_foo.x;
}
Foo& operator = (const Foo& ori_foo) {
std::cout << "operator =" << std::endl;
x = ori_foo.x;
return *this;
}
Foo(int new_x) {
x = new_x;
}
};
class BarParent {
public:
Foo *p_foo;
BarParent(Foo* new_p_foo) : p_foo(new_p_foo)
{
std::cout << (*new_p_foo).x << std::endl;
}
};
class BarChild : public BarParent {
public:
Foo foo; …Run Code Online (Sandbox Code Playgroud) 在使用静态库编译程序时,我从许多来源(包括SO社区)建议我两次包含该库.
如:
gcc main.c -lslA -lslB -lslC -lslA -lslB -o final
Run Code Online (Sandbox Code Playgroud)
这是否会导致更大的可执行文件(.ie是否足够智能,以避免双重包含?).
这是(多重包含)正确的解决方案还是一种解决方法(.ie总会存在更合适的,即使是更难处理它的方法)
int secret_foo(void)
{
int key = get_secret();
/* use the key to do highly privileged stuff */
....
/* Need to clear the value of key on the stack before exit */
key = 0;
/* Any half decent compiler would probably optimize out the statement above */
/* How can I convince it not to do that? */
return result;
}
Run Code Online (Sandbox Code Playgroud)
我需要在执行key之前从堆栈中清除变量的值return(如代码所示).
如果您感到好奇,这是一个实际的客户需求(嵌入式域).
关于rvalue和lvalue,我花了几个小时.这是我的理解
int main()
{
//.....
Foo foo = Bar1();
foo = Bar2();
//......
}
Foo Bar1()
{
//Do something including create foo
return foo;
}
Foo& Bar2()
{
//Do something including create foo
return foo;
}
Run Code Online (Sandbox Code Playgroud)
在c ++ 03下,Bar1()将复制返回对象(就在返回之前),然后返回复制对象的地址; 执行一个即将被销毁的物品的浪费副本.Bar2()将返回在函数内创建的对象.
下C++ 11,Bar1()和Bar2()将基本上相等(并且也等同于Bar2()C++ 03).
是对的吗?如果没有,请详细说明.
对不起,如果这是一个微不足道的问题:
实施1:
class Foo
{
protected: int bar;
public: Foo(int bar)
{
this->bar =bar;
}
};
Run Code Online (Sandbox Code Playgroud)
实施2:
class Foo
{
protected: int bar;
public: Foo(int bar)
{
this.bar =bar;
}
};
Run Code Online (Sandbox Code Playgroud)
实施2的输出:
request for member ‘x’ in ‘this’, which is of pointer type ‘Foo* const’ (maybe you meant to use ‘->’ ?)
所以this是一个指针,这个问题在代码中有语法错误
c++ ×4
c ×2
pointers ×2
c++11 ×1
dependencies ×1
inheritance ×1
lvalue ×1
member ×1
non-volatile ×1
rvalue ×1
this ×1
volatile ×1