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?
我写一个C程序获取一个环境变量的名称并打印出它的内存地址,只需使用getenv()函数来完成这项工作。假设我想拥有PATH的地址位置 - > ./test PATH。但是当我在gdb中调试该程序时,该变量的内存位置是不同的。你能详细解释为什么会有这样的不同吗?
To be more exact:
更确切地说:
./test PATH
--> 0xbffffd96
./test PATH - > 0xbffffd96
debug in gdb --> 0xbffffd53
在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
[编辑]感谢您的解释。我实际上问的是,变量的内存地址(在这种情况下,环境变量)如何随着不同的程序而变化。例如,我有2个程序a.out和b.out
./a.out
--> PATH's address is some number
./a.out - > PATH的地址是一些数字
./b.out
--> another number
./b.out - >另一个号码
So, what causes this difference between 2 numbers? I hope I have clearly demonstrated what I want to ask. Thanks mates.
那么,是什么导致了2个数字之间的差异呢?我希望我已经清楚地证明了我想问的问题。谢谢你的队友。
3 个解决方案
#1
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.
通常,环境变量是某些“过程数据块”的一部分,并且这些变量是从启动过程继承的。如果您在调试器中运行程序,该调试器将拥有自己的过程数据块,您的程序将从调试器继承其过程数据块。这反过来可能继承了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.
无论如何这都无所谓,因为环境变量的接口并没有给你那种细节。例如,在Windows上,当您要求环境变量很可能从Unicode转换为本地8位代码页。你永远不会看到原始变量,只是(近似值)它的值。
#2
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.
我对PATH的所有程序性兴趣都在其内容中,而不是任何地址。
#3
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.
为什么你希望它每次返回相同的内存位置? getenv返回“指向包含指定名称值的字符串的指针。”未指定字符串所在的内存位置,以及稍后是否覆盖该位置。
#1
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.
通常,环境变量是某些“过程数据块”的一部分,并且这些变量是从启动过程继承的。如果您在调试器中运行程序,该调试器将拥有自己的过程数据块,您的程序将从调试器继承其过程数据块。这反过来可能继承了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.
无论如何这都无所谓,因为环境变量的接口并没有给你那种细节。例如,在Windows上,当您要求环境变量很可能从Unicode转换为本地8位代码页。你永远不会看到原始变量,只是(近似值)它的值。
#2
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.
我对PATH的所有程序性兴趣都在其内容中,而不是任何地址。
#3
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.
为什么你希望它每次返回相同的内存位置? getenv返回“指向包含指定名称值的字符串的指针。”未指定字符串所在的内存位置,以及稍后是否覆盖该位置。