复制到指针内存? [英] copy to pointer memory ?
问题描述
嗨
我有以下代码。
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
推荐答案
" MC" <毫安********* @ yahoo.com>写在
< bj ********** @ zcars0v6.ca.nortel.com>:
"MC" <ma*********@yahoo.com> wrote in
<bj**********@zcars0v6.ca.nortel.com>:
嗨
我有以下代码。
int * p;
p = malloc(10);
int * j;
j = p;
免费(p);
这是合法的吗?
是的。
还会指向与之前指出的相同的内存吗?
是的,p仍然指向那里。
我可以使用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 ? Yes.
will j still point to the same
memory that p pointed to earlier ? Yes, and p still points there, too.
Can I access that memory using j ? No. You are not allowed to use it, since you have already
free()''d it.
I read somewhere that even though free is
called, the memory is still available. This is not guaranteed by any means - it invokes undefined behaviour.
If you try to acces the memory, /anything/ can happen.
so in my case j should still point to that
memory right ? Right, but see above.
MC
Irrwahn
-
今天没有信号。
Irrwahn
--
No sig today.
2003年9月3日星期三,MC写道:
On Wed, 3 Sep 2003, MC wrote:
嗨
我有以下代码。
int * p;
p = malloc(10);
int * j;
j = p;
free(p);
我可以用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
[来自常见问题]
--------------------------------- -----------------------------------
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
MC写道:
您好
我有以下代码。
int * p;
p = malloc(10);
int * j;
j = p;
免费(p);
这合法吗?
在C99中,是的。在C89中,no:可执行语句不能在声明之前使用
。但是一个简单的重新排列使得它在C89中也是合法的:
int * p;
int * j;
p = malloc(10);
j = p;
free(p);
将j指向p先前指出的记忆是什么?
号。更准确地说,符合
计划不可能找到,这意味着任何试图找出
都会调用未定义的行为。
我可以使用j访问该内存吗?
编号房屋;这是malloc()调用。在一张名为p的纸条上写下它的地址。制作一张
的那张纸条的复印件并称之为j。给你友好的当地纵火犯
`p''让他在指定的地址烧掉房子。
你口袋里还有'j''的事实并不代表
烧毁的房子是适合居住的。
我在某个地方看到即使被调用免费,内存仍然可用。
我在某处看到地球是扁平的。
所以在我的情况下j应该仍然指向那个
记忆吧?
Hi
I have the following piece of code.
int *p;
p = malloc(10);
int *j;
j = p;
free(p);
Is this legal ?
In C99, yes. In C89, no: executable statements cannot
precede declarations. But a simple rearrangement makes it
legal in C89, too:
int *p;
int *j;
p = malloc(10);
j = p;
free (p);
will j still point to the same
memory that p pointed to earlier ?
No. More precisely, "It''s impossible for a conforming
program to find out," meaning that any attempt to find out
invokes undefined behavior.
Can I access that memory using j ?
No. Build a house; that''s the malloc() call. Write
down its address on a slip of paper called `p''. Make a
photocopy of that slip of paper and call it `j''. Give
`p'' to your friendly local arsonist and have him burn
down the house at the indicated address. The fact that
you''ve still got `j'' in your pocket doesn''t mean the
burned-out house is habitable.
I read somewhere that even though free is
called, the memory is still available.
I read somewhere that the Earth is flat.
so in my case j should still point to that
memory right ?
这是不可能的。尝试使用以前指向的内存`j''
(它已被烧毁,还记得吗?)并且没有告诉可能发生的事情。
推荐阅读:comp.lang.c的第7节经常
问答(FAQ)列表
-
呃*********@sun.com
这篇关于复制到指针内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!