繁体   English   中英

为什么全局函数可以用两个不同的地址调用,而全局 int 只有一个,而 static 函数也有一个?

[英]Why global funcs can be called with two different addresses but global int has only one, but static funcs have one too?

这是一个简单的代码示例:

#include <iostream>

int hello_world(float j) {
    std::cout << j << std::endl;
    return 12;
}

int myvar = 123;

int main() {
    // vs19 debugger watch: (myvar_addr == &myvar) is true
    int myvar_addr = (int)&myvar;

    // vs19 debugger watch: (hw_addr == &hello_world) is false, but function on this new address is hello_world
    int hw_addr = (int)&hello_world;

    auto hw_func = (int (*)(float))hw_addr;
    hw_func(10);

    int myvar_value = *((int*)myvar_addr);

    system("pause"); // breakpoint
}

如您所见 - 在 main 内部检索到的hello_world function 的地址和我在调试器中看到的地址 - 是两个不同的地址,但对于myvar它们是相同的,为什么? 当我将static关键字添加到 function 声明时 - 这两个地址变得相等,为什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM