由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c ptr question
相关主题
dereference a NULL pointer in Cany good open source memory pool for C
请教两道linux面试题目C一个问题搞不懂
请教函数 INIT 怎么能free memory一个简单的小问题
这段C++程序有错吗?C++ Q05: pointer to constant variable
问个程序问题a c++ question
一道面试题Why this is a dangling pointer
超牛的debugA weird C programming language--Return a pointer to array
请问可以这样定义struct吗?size不固定的struct怎么定义呀?
相关话题的讨论汇总
话题: np话题: struct话题: tmp话题: pointer话题: next
进入Programming版参与讨论
1 (共1页)
m******t
发帖数: 4077
1
非常简单一个程序
struct B {
struct B *next;
}
struct A {
int C;
struct B;
}
int
main()
{
struct B ** np;
tmp = (struct A *) malloc (sizeof(struct A));
np = &(tmp->B->next);
free(tmp);
printf("*np is %p", *np);
return 0;
}
按说free了tmp以后B->next就不再存在了,对吧?为什么这个时候还能够正确的
dereference *np呢?
如果把struct A中的B和C换个位置,*np就正确的变成的NULL pointer了。
是不是free memory的时候不wipe memory啊?
X****r
发帖数: 3557
2
是不是free memory的时候不wipe memory啊?是

【在 m******t 的大作中提到】
: 非常简单一个程序
: struct B {
: struct B *next;
: }
: struct A {
: int C;
: struct B;
: }
: int
: main()

m******t
发帖数: 4077
3
那为啥把B和C的位置换换就被弄成NULL了? linxu/gcc

【在 X****r 的大作中提到】
: 是不是free memory的时候不wipe memory啊?是
X****r
发帖数: 3557
4
The result of dereferencing a dangling pointer is undefined,
meaning it could be anything, including 0. Or segfault.
There is nothing meaningful can be deduced. Just don't do it.

【在 m******t 的大作中提到】
: 那为啥把B和C的位置换换就被弄成NULL了? linxu/gcc
y***d
发帖数: 2330
5
try get it compilable first...

【在 m******t 的大作中提到】
: 非常简单一个程序
: struct B {
: struct B *next;
: }
: struct A {
: int C;
: struct B;
: }
: int
: main()

m******t
发帖数: 4077
6
这当然是psudo code了。compile是没有问题的。

【在 y***d 的大作中提到】
: try get it compilable first...
t****t
发帖数: 6806
7
your *np is indeterminated from the beginning -- you never assigned to B.
next.
y***d
发帖数: 2330
8
Sorry but I have to say that this psudo code is completely trash.
"tmp->B->next" can not be applied to member A.B.
What really matters is whether A.B is a pointer or not.
Without knowing what the type of A.B is, one can not talk about '*np就正确的
变成的NULL pointer了'.
BTW, I don't think there is something like *np should 'correctly' be,
because 'free' can do
anything it likes to.

【在 m******t 的大作中提到】
: 这当然是psudo code了。compile是没有问题的。
m******t
发帖数: 4077
9
see ur point. should be tmp->B.next
A.B is of course not a pointer. It is a structure composed of a single
pointer.
tmp->B.next is not assigned to a member of A.B either but it is a valid
pointer. It points to the next member of the list.
Its address though is assigned to np; thus *np is exactly tmp->B->next if
you dereference it before free.

【在 y***d 的大作中提到】
: Sorry but I have to say that this psudo code is completely trash.
: "tmp->B->next" can not be applied to member A.B.
: What really matters is whether A.B is a pointer or not.
: Without knowing what the type of A.B is, one can not talk about '*np就正确的
: 变成的NULL pointer了'.
: BTW, I don't think there is something like *np should 'correctly' be,
: because 'free' can do
: anything it likes to.

t****t
发帖数: 6806
10
看重点就行了, 问的人都不care, 你care什么啊.

【在 y***d 的大作中提到】
: Sorry but I have to say that this psudo code is completely trash.
: "tmp->B->next" can not be applied to member A.B.
: What really matters is whether A.B is a pointer or not.
: Without knowing what the type of A.B is, one can not talk about '*np就正确的
: 变成的NULL pointer了'.
: BTW, I don't think there is something like *np should 'correctly' be,
: because 'free' can do
: anything it likes to.

y***d
发帖数: 2330
11
So np points into the chunk of memory allocated.
So just forget about np once 'free(tmp)' is called, since you've already
discarded this chunk of memory.

【在 m******t 的大作中提到】
: see ur point. should be tmp->B.next
: A.B is of course not a pointer. It is a structure composed of a single
: pointer.
: tmp->B.next is not assigned to a member of A.B either but it is a valid
: pointer. It points to the next member of the list.
: Its address though is assigned to np; thus *np is exactly tmp->B->next if
: you dereference it before free.

1 (共1页)
进入Programming版参与讨论
相关主题
size不固定的struct怎么定义呀?问个程序问题
初学C,对什么该free一直搞不明白一道面试题
gcc编译出错,attribute问题?超牛的debug
地址空间里的一个BYTE不能写入(是合法地址)请问可以这样定义struct吗?
dereference a NULL pointer in Cany good open source memory pool for C
请教两道linux面试题目C一个问题搞不懂
请教函数 INIT 怎么能free memory一个简单的小问题
这段C++程序有错吗?C++ Q05: pointer to constant variable
相关话题的讨论汇总
话题: np话题: struct话题: tmp话题: pointer话题: next