问题描述
嗨
我有以下代码。
int * p;
p = malloc(10);
int * j;
j = p;
免费(p);
这是合法吗? j还会指向前面提到的相同的内存吗?
我可以使用j访问该内存吗?
我在某个地方读过,虽然免费是
,但内存仍然可用。
所以在我的情况下j应该仍然指向那个
记忆吧?
MC
Hi
I have the following piece of code.
int *p;
p = malloc(10);
int *j;
j = p;
free(p);
Is this legal ? will j still point to the same
memory that p pointed to earlier ?
Can I access that memory using j ?
I read somewhere that even though free is
called, the memory is still available.
so in my case j should still point to that
memory right ?
MC
推荐答案
Irrwahn
-
今天没有信号。
Irrwahn
--
No sig today.
[来自常见问题]
--------------------------------- -----------------------------------
7.20:你不能你释放后使用动态分配的内存,
你可以吗?
答:不会。某些早期的malloc()文档说明了
释放内存的内容未受干扰,但是这个错误的建议保证从来都不是通用的,而且不需要
C标准。
很少有程序员会使用故意释放内存的内容
,但很容易这么做。考虑
以下(正确)代码,用于释放单链表:
struct list * listp,* nextp;
for(listp = base; listp!= NULL; listp = nextp){
nextp = listp-> next;
free(listp);
}
并注意如果更明显的循环迭代将会发生什么
表达式listp = listp-> next使用了,没有临时
nextp指针。
参考文献:K& R2 Sec。 7.8.5 p。 167; ISO秒7.10.3;理由
秒4.10.3.2; H& S Sec。 16.2 p。 387; CT& P Sec。 7.10 p。 95.
7.21:为什么调用free()后指针无效?
使用(赋值,比较)指针是多么不安全价值之后
它被释放了吗?
答:当你调用free()时,传递的内存是指
指针被释放,但调用者指针
的值可能保持不变,因为C'的值传递语义
表示调用函数永远不会永久地改变它们的参数的价值
。 (另见问题4.8。)
严格来说,已释放的指针值无效,* b $ b无效,*任何*使用它,甚至如果没有被解除引用,理论上可以导致麻烦,虽然作为一个质量的实施问题,大多数实现可能都不会出现
out他们的方式为无害的指针无害使用产生例外。
参考文献:ISO Sec。 7.10.3;基本原理3.2.2.3。
====================================== ============ =================
希望这会有所帮助,
-dj
[ from the FAQ ]
--------------------------------------------------------------------
7.20: You can''t use dynamically-allocated memory after you free it,
can you?
A: No. Some early documentation for malloc() stated that the
contents of freed memory were "left undisturbed," but this ill-
advised guarantee was never universal and is not required by the
C Standard.
Few programmers would use the contents of freed memory
deliberately, but it is easy to do so accidentally. Consider
the following (correct) code for freeing a singly-linked list:
struct list *listp, *nextp;
for(listp = base; listp != NULL; listp = nextp) {
nextp = listp->next;
free(listp);
}
and notice what would happen if the more-obvious loop iteration
expression listp = listp->next were used, without the temporary
nextp pointer.
References: K&R2 Sec. 7.8.5 p. 167; ISO Sec. 7.10.3; Rationale
Sec. 4.10.3.2; H&S Sec. 16.2 p. 387; CT&P Sec. 7.10 p. 95.
7.21: Why isn''t a pointer null after calling free()?
How unsafe is it to use (assign, compare) a pointer value after
it''s been freed?
A: When you call free(), the memory pointed to by the passed
pointer is freed, but the value of the pointer in the caller
probably remains unchanged, because C''s pass-by-value semantics
mean that called functions never permanently change the values
of their arguments. (See also question 4.8.)
A pointer value which has been freed is, strictly speaking,
invalid, and *any* use of it, even if is not dereferenced, can
theoretically lead to trouble, though as a quality of
implementation issue, most implementations will probably not go
out of their way to generate exceptions for innocuous uses of
invalid pointers.
References: ISO Sec. 7.10.3; Rationale Sec. 3.2.2.3.
================================================== =================
Hope this helps,
-dj
这是不可能的。尝试使用以前指向的内存`j''
(它已被烧毁,还记得吗?)并且没有告诉可能发生的事情。
推荐阅读:comp.lang.c的第7节经常
问答(FAQ)列表
http://www.eskimo.com/~scs/C-faq /top.html
-
这篇关于复制到指针内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!