C++当"extern int * name"引用不存在的变量,然后访问"name"时会发生什么

C++ what happens when `extern int * name` refers to a variable that doesn't exist, and then `name` gets accessed

本文关键字:name 访问 然后 什么 int extern 引用 C++ 不存在 变量      更新时间:2023-10-16

我在.cpp中有一个extern unsigned int * name,然后我注释掉了它引用的指针(在其他地方声明)。令我惊讶的是,程序仍然运行而没有崩溃,并且实际上按预期工作,而且我确信name正在被取消引用。那么在这种情况下到底发生了什么呢?

您错过了一些东西,因为在这种情况下链接过程应该失败。如果您提供任何具体的可编译代码示例,将更清楚发生了什么。

或者您错把name解引用了,或者您在其他地方添加了一个相同类型的新name