It is modifiable, and gdb
with -std=11
shows what happens
国际标准化组织C11,2011年国际标准化组织C标准的修订。 这一标准得到了完全的支持。 因此,让我们安全地假设,电梯汇编者支持C11标准文件提出的大多数特征。 选择——C11标准将编制方案。
argv[]
is just array of string pointers
like arguments of any other function they are
treated as local variables and are modifiable.
Since they belong to main() function they will last until program
exits..
<代码>char **argv (pointer to pointer)和int argc
均为main(
function,从而在一栏中形成。
If we run this code and stop at breakpoint at main():
:~/proba$ gdb --args proba BBBBBBB CCCCCCC
#include <stdio.h>
int main(int argc, char **argv)
{
char buf[20] = "AAAAAAA";
argv[0] = buf;
return 1;
}
Starting program: /home/drazen/proba/proba BBBBBBB CCCCCCC
Breakpoint 1, main (argc=3, argv=0x7fffffffdef8) at main9.c:5
如果我们 dump倒,我们看到:
(gdb) x/32gx $sp
0x7fffffffddb0: 0x00007fffffffdef8 0x0000000300000000
0x7fffffffddc0: 0x0000000000000000 0x0000000000000000
0x7fffffffddd0: 0x0000000000000000 0x0000000000000000
.......
We recognize value of argv
0x00007fffffffdef8
and value of argc
0x00000003
on top of a stack.
由于我们通过了一个论点<代码>argc,按预期为3。
But argv
holds address of the array of char pointers.
So at address 0x00007fffffffdef8
is actually address of the first pointer argv[0]
with value 0x00007fffffffe25c
,
what should be an address where program name with absolute path is.
And at address 0x7fffffffdf00
is second pointer argv[1]
with value 0x00007fffffffe275
what should be an address where first program argument is.
And at address 0x7fffffffdf08
is third pointer argv[2]
with value 0x00007fffffffe27d
what should be an address where second program argument is.
我们可以看到:
........
0x7fffffffdef0: 0x0000000000000003 0x00007fffffffe25c<argv[0]>
0x7fffffffdf00: 0x00007fffffffe275<argv[1]> 0x00007fffffffe27d<argv[2]>
http://code>argv[0] 。
(gdb) x/s 0x00007fffffffe25c
0x7fffffffe264: "/home/drazen/proba/proba"
地址:0x00007fffe275 。 BBBBBBB
:
(gdb) x/s 0x00007fffffffe275
0x7fffffffe27d: "BBBBBBB"
And at address 0x00007fffffffe27d
is third argument argv[2]
which holds address of a program argument CCCCCCC
:
(gdb) x/s 0x00007fffffffe27d
0x7fffffffe27d: "CCCCCCC"
我们看到,标明这些字面的字面编号为<>char*argv[]点数,以连续地址标示:
(gdb) x/3s 0x00007fffffffe25c
0x7fffffffe25c: "/home/drazen/proba/proba"
0x7fffffffe275: "BBBBBBB"
0x7fffffffe27d: "CCCCCCC"
Now let s step few instructions where we initialize local string
variable on stack and reassign pointerargv[0]
:
(gdb) s
6 char buf[20] = "AAAAAAA";
(gdb) s
7 argv[0] = buf;
(gdb) s
8 return 1;
If we dump the stack now we notice it has slightly changed.
We see that local variable buf[]
was initialized on stack too (string AAAAAAA
is HEX 0x0041414141414141
) right after
main()
function arguments:
(gdb) x/32gx $sp
0x7fffffffddb0: 0x00007fffffffdef8 0x0000000300000000
0x7fffffffddc0: 0x0041414141414141 0x0000000000000000
0x7fffffffddd0: 0x0000000000000000 0x5315d27018aa8e00
.....
.....
0x7fffffffdef0: 0x0000000000000003 0x00007fffffffddc0<argv[0]>
0x7fffffffdf00: 0x00007fffffffe275 0x00007fffffffe27d
Value 0x00007fffffffdef8
of argv
hasn t changed, but value of argv[0]
has changed to 0x00007fffffffddc0
:
.....
0x7fffffffdef0: 0x0000000000000003 0x00007fffffffddc0<argv[0]>
0x7fffffffdf00: 0x00007fffffffe275 0x00007fffffffe27d
(gdb) x/s 0x00007fffffffddc0
0x7fffffffddc0: "AAAAAAA"
(gdb) p argv[0]
$1 = 0x7fffffffddc0 "AAAAAAA"
pointer argv[0]
now points to the new memory location where string literal "AAAAAAA" is allocated on a stack.