- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我在翻阅我的 C 编程教科书时看到了这段代码。
#include <stdio.h>
int j, k;
int *ptr;
int main(void)
{
j = 1;
k = 2;
ptr = &k;
printf("\n");
printf("j has the value %d and is stored at %p\n", j, (void *)&j);
printf("k has the value %d and is stored at %p\n", k, (void *)&k);
printf("ptr has the value %p and is stored at %p\n", (void *)ptr, (void *)&ptr);
printf("The value of the integer pointed to by ptr is %d\n", *ptr);
return 0;
}
我运行了它,输出是:
j has the value 1 and is stored at 0x4030e0
k has the value 2 and is stored at 0x403100ptr has the value 0x403100 and is stored at 0x4030f0
The value of the integer pointed to by ptr is 2
我的问题是,如果我没有通过编译器运行它,您如何仅通过查看这段代码就知道这些变量的地址?我只是不确定如何获取变量的实际地址。谢谢!
最佳答案
这是我的理解:
C 中内存中事物的绝对地址是未指定的。它没有标准化为语言。正因如此,光看代码是无法知道事物在内存中的位置的。 (但是,如果您使用相同的编译器、代码、编译器选项、运行时和操作系统,则地址可能是一致的。)
当您开发应用程序时,这不是您应该依赖的行为。但是,在某些情况下,您可能会依赖于两个事物的位置之间的差异。例如,您可以确定指向两个数组元素的指针地址之间的差异,以确定它们相隔多少个元素。
顺便说一句,如果您正在考虑使用变量的内存位置来解决特定问题,您可能会发现发布一个单独的问题来询问如何在不依赖这种行为的情况下这样做会很有帮助。
关于c - 你怎么知道变量的确切地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262893/
通过多次搜索和pytorch文档本身,我可以发现在嵌入层内部有一个查找表,用于存储嵌入向量。我无法理解的是: 在这一层的培训期间究竟发生了什么? 权重是多少,以及这些权重的梯度是如何计算的? 我的直觉
当应用程序有大量数据(400M)要写入非阻塞套接字时,write() 返回EWOULDBLOCK 或EAGAIN 当发送缓冲区变满时。 当套接字被(e)轮询时,我有时会看到发送缓冲区中有 7M 空间(
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我经常阅读一些编程语言对模块的支持(“一流”)(OCaml,Scala,TypeScript [?]),并且最近偶然发现了这样的答案,即在Scala的显着特征中引用模块作为一流公民。 我以为我很清楚模
我已经提交了一个自定义的开放图表故事,但它被拒绝并显示以下消息: 打开图表捐赠(行动类型)提交内容、操作、对象和使用说明必须以英文提交。您可以在应用程序面板的“本地化”选项卡中翻译操作和对象。 如果我
给定一个任意的 boolean 值列表,确定其中一个恰好为真的最优雅的方法是什么? 最明显的 hack 是类型转换:将 false 转换为 0,将 true 转换为 1,然后对它们求和,并返回 sum
这个问题在这里已经有了答案: When to use extern "C" in simple words? [duplicate] (7 个答案) 关闭 9 年前。 如果您想将此问题标记为重复问题
我是一名优秀的程序员,十分优秀!