我遇到了这段代码:
void f(const std::string &s);
Run Code Online (Sandbox Code Playgroud)
然后打个电话:
f( *((std::string*)NULL) );
Run Code Online (Sandbox Code Playgroud)
我想知道其他人对这种结构的看法,它用于表示函数f()应该使用一些默认值(它计算)而不是一些用户提供的值.
我不知道该怎么想,它看起来很奇怪,但你觉得这个结构怎么样?
假设我有多个线程访问相同的内存位置。而且,如果有的话,它们都写入相同的值,但没有人读取它。之后,所有线程(通过锁)收敛,然后我才读取值。我需要为此使用原子吗?这是针对 x86_64 系统的。该值是一个 int32。
在 C 中,当您有一个函数返回指向它的局部(在堆栈上)变量之一的指针时,调用函数会返回 null。为什么会这样?
我可以在我的硬件上用 C 语言做到这一点
void A() {
int A = 5;
}
void B() {
// B will be 5 even when uninitialised due to the B stack frame using
// the old memory layout of A
int B;
printf("%d\n", B);
}
int main() {
A();
B();
}
Run Code Online (Sandbox Code Playgroud)
由于堆栈帧内存没有被重置,并且 B 覆盖了堆栈中 A 的内存记录。
但是我做不到
int* C() {
int C = 10;
return &C;
}
int main() {
// D will be null ?
int* D = C(); …
Run Code Online (Sandbox Code Playgroud) 问题问,
让 int x = 1,找到一个 int y 的值,其中以下语句将返回 false:
(x < y) == (-x > -y)
我知道答案应该是 4 个字节长(8 个十六进制数字),但我不知道如何解决这个问题。