为什么程序运行时环境变量的内存地址不一样

发布于 2024-07-26 01:28:37 字数 521 浏览 3 评论 0原文

我编写了一个 C 程序,获取环境变量的名称并打印出它的内存地址,只需使用 getenv() 函数即可完成这项工作。 假设我想要 PATH 的地址位置 --> ./测试路径。 但是当我在 gdb 中调试该程序时,该变量的内存位置不同。 您能详细解释一下为什么会有这样的不同吗?

更准确地说:

./test PATH --> 0xbffffd96

在 gdb 中调试 --> 0xbffffd53

[编辑] 感谢您的解释。 我真正关心的是,变量(在本例中为环境变量)的内存地址如何随着不同的程序而变化。 例如,我有2个程序a.out和b.out

./a.out --> PATH 的地址是一些数字

./b.out --> 另一个数字

那么,是什么导致了两个数字之间的差异呢? 我希望我已经清楚地表达了我想问的问题。 谢谢伙伴们。

I write a C program gets in an environment variable's name and print out it's memory address, simply use the getenv() function to do the job. Let's say I wanna have the address location of PATH --> ./test PATH. But when I debug that program in gdb, the memory location of that variable is different. Can you explain in detail why is there such a different?

To be more exact:

./test PATH --> 0xbffffd96

debug in gdb --> 0xbffffd53

[edit] Thanks for your explanations. What I actually in question about is, how the memory address of a variable (in this case, an environment variable) changes with different programs. For example, I have 2 program a.out and b.out

./a.out --> PATH's address is some number

./b.out --> another number

So, what causes this difference between 2 numbers? I hope I have clearly demonstrated what I want to ask. Thanks mates.

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

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

发布评论

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

评论(3

七七 2024-08-02 01:28:37

通常,环境变量是某些“进程数据块”的一部分,并且它们是从启动进程继承的。 如果您在调试器中运行程序,则该调试器将拥有自己的进程数据块,并且您的程序将从调试器继承其进程数据块。 这反过来可能继承了 IDE 的程序数据块。

无论如何,这并不重要,因为环境变量的接口不会为您提供此类详细信息。 例如,在 Windows 上,当您请求环境变量时,环境变量很可能会从 Unicode 转换为本地 8 位代码页。 您永远不会看到原始变量,而只能看到其值(的近似值)。

Typically, environment variables are part of some "process data block", and those are inherited from the starting process. If you are running a program in a debugger, that debugger will have its own process data block and your program will inherit its process data block from the debugger. That in turn might have inherited the program data block of the IDE.

This doesn't matter anyway, because the interface to the environment variables doesn't give you that kind of details. For instance, on Windows it's quite likely that the environment variables will be converted from Unicode to your local 8 bit codepage when you ask for them. You'd never see the original variable, just (an approximation of) its value.

蓬勃野心 2024-08-02 01:28:37

也许你想做什么?

printf("%s",getenv("PATH")); 

获取环境变量字符串确实有意义。
但是,系统为您提供字符串的地址在任何地方都没有相关性
(特别是在本计划范围之外)。

您应该对环境字符串值而不是其地址感兴趣
如果您有任何理由使用该地址,请在此处给出。

例如,

echo $PATH

给我,

/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin: ... etc

我对 PATH 的所有编程兴趣都在于其内容而不是任何类型的地址。

Perhaps you want to do?

printf("%s",getenv("PATH")); 

Getting the environment variable string does make sense.
But, the address where the system gives you the string, has no relevance anywhere
(particularly outside the scope of this program).

You should be interested in the environment string value rather than its address.
If you have any reason to use the address, please give that here.

For example,

echo $PATH

gives me,

/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin: ... etc

All my programmatic interest with PATH would be in its contents not any sort of address.

野鹿林 2024-08-02 01:28:37

为什么你希望它每次都返回相同的内存位置? getenv 返回“指向包含指定名称的值的字符串的指针”。 没有指定字符串位于哪个内存位置,也没有指定该位置稍后是否会被覆盖。

Why would you expect it to return the same memory location every time? getenv returns "a pointer to a string containing the value for the specified name." It is not specified which memory location the string is located at, nor whether that location will later be overwritten.

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