gpt4 book ai didi

c - C函数是否保证有固定的内存地址?

转载 作者:太空狗 更新时间:2023-10-29 16:40:49 26 4
gpt4 key购买 nike

如果我存储一个指向函数的指针,然后在我的程序执行期间的某个稍后时间点,将它与同一函数的地址进行比较,这两个地址是否保证相等。

例如

int foo(void){return 0;}
int (*foo_p)(void) = &foo;

assert(foo_p == &foo);

在上面的代码中断言总是保证成功吗?是否存在函数地址可以更改的任何情况?

最佳答案

根据 6.5.9:

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

(加黑体表示强调。)

关于c - C函数是否保证有固定的内存地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7350054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com