在这段代码中,依赖于gdb
,p
从分配0x602010
到0x0
何时更改NULL
,(如我所料)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a = 10;
// gdb output
int *p = (int *) malloc(sizeof(int)); // p = (int *) 0x602010
p = NULL; // p = (int *) 0x0
p = &a; // p = p = (int *) 0x7fffffffe15c
return 0;
}
但是,当在 inp
之外更改时,我想它不会更改为,我不知道为什么:main()
task()
0x0
#include<stdio.h>
#include<stdlib.h>
void tast(int *p);
void task(int *p)
{
/*
before
(gdb) p p
$1 = (int *) 0x7fffffffe15c (same as variable a)
(gdb) p &p
$2 = (int **) 0x7fffffffe128
*/
p = NULL;
/*
after
(gdb) p p
$3 = (int *) 0x7fffffffe15c no change?
(gdb) p &p
$4 = (int **) 0x7fffffffe128
*/
}
int main()
{
int a = 10;
// gdb output
int *p = (int *) malloc(sizeof(int)); // p = (int *) 0x602010
p = NULL; // p = (int *) 0x0
p = &a; // p = p = (int *) 0x7fffffffe15c
// it is possible to change what p points to
// after calling task()?
task(p);
// p will be NULL?
return 0;
}
为什么 p 在 task() 中没有变为 0x0?