为什么这个 volatile 变量的地址总是为 1?

发布于 2024-12-17 17:26:06 字数 121 浏览 7 评论 0原文

我想检查变量的地址

volatile int clock;
cout << &clock;

,但它总是说 x 位于地址 1。我做错了什么吗?

I wanted to inspect the address of my variable

volatile int clock;
cout << &clock;

But it always says that x is at address 1. Am i doing something wrong??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戴着白色围巾的女孩 2024-12-24 17:26:06

iostreams 会将大多数指针转换为 void * 进行显示 - 但 易失性 指针不存在转换。因此,C++ 退回到隐式转换为 bool。如果要打印地址,请显式转换为 void*

std::cout << (void*)&clock;

iostreams will cast most pointers to void * for display - but no conversion exists for volatile pointers. As such C++ falls back to the implicit cast to bool. Cast to void* explicitly if you want to print the address:

std::cout << (void*)&clock;
携余温的黄昏 2024-12-24 17:26:06

const void* 有一个 operator<<,但没有 volatile void*operator<< ,并且隐式转换不会删除 volatile (也不会删除 const)。

正如 GMan 所说,所指向类型的简历资格应该与打印地址的业务无关。也许27.7.3.6.2中定义的重载应该是operator<<(const volatile void* val);,我无法立即看到任何缺点。但事实并非如此。

#include <iostream>

void foo(const void *a) {
    std::cout << "pointer\n";
}

void foo(bool a) {
    std::cout << "bool\n";
}

int main() {
    volatile int x;
    foo(&x);
    std::cout << &x << "\n";
    int y;
    foo(&y);
    std::cout << &y << "\n";
    void foo(volatile void*);
    foo(&x);
}

void foo(volatile void *a) {
    std::cout << "now it's a pointer\n";
}

输出:

bool
1
pointer
0x22cd28
now it's a pointer

There's an operator<< for const void*, but there's no operator<< for volatile void*, and the implicit conversion will not remove volatile (it won't remove const either).

As GMan says, the cv-qualification of the type pointed to should be irrelevant to the business of printing an address. Perhaps the overload defined in 27.7.3.6.2 should be operator<<(const volatile void* val);, I can't immediately see any disadvantage. But it isn't.

#include <iostream>

void foo(const void *a) {
    std::cout << "pointer\n";
}

void foo(bool a) {
    std::cout << "bool\n";
}

int main() {
    volatile int x;
    foo(&x);
    std::cout << &x << "\n";
    int y;
    foo(&y);
    std::cout << &y << "\n";
    void foo(volatile void*);
    foo(&x);
}

void foo(volatile void *a) {
    std::cout << "now it's a pointer\n";
}

Output:

bool
1
pointer
0x22cd28
now it's a pointer
别再吹冷风 2024-12-24 17:26:06

这是因为没有重载操作符<<,它接受一个指向易失性的指针,并且没有可以满足它的指针转换。

根据C++标准,

对于任何类型T,指向T的指针、指向const T的指针以及指向易失性T的指针被视为不同的参数类型,例如对 T 的引用、对 const T 的引用以及对 volatile T 的引用。

运算符 << 对于指向非-静态成员、指向易失性的指针或函数指针,因此尝试输出此类对象会调用到 bool 的隐式转换。

This is because there is no overload for operator << that takes a pointer to volatile, and there is no pointer conversion that could satisfy it.

According to C++ standard,

for any type T, pointer to T, pointer to const T, and pointer to volatile T are considered distinct parameter types, as are reference to T, reference to const T, and reference to volatile T.

Operator << has no overload for pointers to non-static member, pointers to volatile, or function pointers, so attempting to output such objects invokes implicit conversion to bool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文