为什么在运行程序时环境变量的内存地址有差异 [英] Why is there a difference in memory address of environment variable when running a program

查看:109
本文介绍了为什么在运行程序时环境变量的内存地址有差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

更确切地说:

./test PATH-> 0xbffffd96

./test PATH --> 0xbffffd96

在gdb中调试-> 0xbffffd53

debug in gdb --> 0xbffffd53

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

[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的地址是某个数字

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

./b.out->其他号码

那么,是什么原因导致2个数字之间出现这种差异?我希望我已经清楚地表明了我想问的问题.谢谢队友.

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

推荐答案

通常,环境变量是某些过程数据块"的一部分,并且这些环境变量是从启动过程继承的.如果您正在调试器中运行程序,则该调试器将具有其自己的过程数据块,并且您的程序将从调试器继承其过程数据块.继而可能继承了IDE的程序数据块.

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.

这没关系,因为与环境变量的接口并没有为您提供此类详细信息.例如,在Windows上,当您请求环境变量时,很可能会将环境变量从Unicode转换为本地8位代码页.您将永远不会看到原始变量,只是它的值(近似值).

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.

这篇关于为什么在运行程序时环境变量的内存地址有差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆