Why there is a difference in the memory address of the environment variable when starting the program

I am writing a C program that gets the name of an environment variable and prints its memory address, just use the function getenv() to complete the job. Say I want to have the address location PATH → ./test PATH. But when I debug this program in gdb, the memory location of this variable is different. Can you explain in detail why there is such a different?

More precisely:

./test PATH → 0xbffffd96

debug in gdb -> 0xbffffd53

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

./a.out → The PATH address is a number

./b.out → another number

So what causes this difference between the two digits? Hope I have clearly demonstrated what I want to ask. Thank you comrades.

+2
source share
3 answers

Typically, environment variables are part of some “process data block”, and they inherit from the initial process. If you run the program in the debugger, this debugger will have its own process data block, and your program will inherit the process data block from the debugger. This, in turn, could inherit the data block of the IDE program.

, . , Windows , Unicode 8- , . , () .

+4

, ?

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

.
, , , ... ( ).

, .
, , .

,

echo $PATH

,

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

PATH - .

+3

Why do 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 indicated in which memory cell the line is located, and whether this place will not be overwritten later.

+1
source

All Articles