我可以在免费(p)之后访问'p'; [英] can i access 'p' even after free(p);

查看:61
本文介绍了我可以在免费(p)之后访问'p';的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在下面的代码中,我发现即使我已经将b / b
释放,我也可以访问p。

not not只有在将内存重新分配给p后,才能访问存储在p中的先前值,即使是
。请有人向我解释一下。

malloc和free在这里做什么?

谢谢,

hassan

#include< stdio.h>

#include< stdlib.h>

#include< math.h>


main()

{

int i,n,* p;

printf(" \ n给出值为n:);

scanf("%d",& n);


p =(int *)malloc(n * sizeof( int));

printf(" \ n给%d整数:",n);

for(i = 0; i< n; i ++) {

printf(" \ nReadig int ...");

scanf("%d",(p + i));

printf(" \\\
The value is%d",*(p + i));

}

printf(" \ n给定的整数是:);

free(p);

p =(int *)malloc(n * sizeof(int));

for(i = 0; i< n; i ++)

printf("%d",*(p + i));

/ / free(p);

printf(" \ n\ n");

return 0;

}


in the code below i find that i am able to access p even after i have
freed it.
not only that the previous values stored in p are accessible even
after reallocation of memory to p. please some one explain it to me.
what does malloc and free do here?
thanks,
hassan

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

main()
{
int i,n,*p;
printf("\n give value for n: ");
scanf("%d", &n);

p= (int *)malloc(n*sizeof(int ));
printf("\n give %d integers: ", n);
for(i=0;i<n;i++){
printf("\nReadig int...");
scanf("%d", (p+i));
printf("\nThe value is %d ", *(p+i));
}
printf("\n the given integers are: ");
free(p);
p= (int *)malloc(n*sizeof(int ));
for(i=0;i<n;i++)
printf("%d ", *(p+i));
//free(p);
printf("\n\n");
return 0;
}

推荐答案

在文章< e2 ************* *************@posting.google.com>,

Hassan Iqbal写道:
In article <e2**************************@posting.google.com >,
Hassan Iqbal wrote:

在下面的代码我发现即使在我释放它之后我也能够访问p。
在将内存重新分配给p之后,不仅可以访问存储在p中的先前值。请有人向我解释一下。
malloc和free在这里做什么?
谢谢,
hassan


请参阅常见问题解答:
http://www.eskimo.com /〜scs/C-faq/q7.20.html


#include< stdio.h>
#include< stdlib.h>
#include< math.h>

main()


main()应始终显式声明为返回int。


{
int i,n,* p;
printf(" \ n赋予n的值:");
scanf("%d", & n);

p =(int *)malloc(n * sizeof(int));
printf(" \ n给%d整数:",n) ;
for(i = 0; i< n; i ++){
printf(" \ nReadig int ...");
scanf("%d", p + i));
printf(" \\\
The value is%d",*(p + i));
}
printf(" \\\
the given整数是:);


奇怪的约定总是在开头输出换行符

的字符串......


免费( p);
p =(int *)malloc(n * sizeof(int));


不要强制转换malloc()的返回值。


for(i = 0; i< n; i ++)
printf("%d",*(p + i));

in the code below i find that i am able to access p even after i have
freed it.
not only that the previous values stored in p are accessible even
after reallocation of memory to p. please some one explain it to me.
what does malloc and free do here?
thanks,
hassan
See the FAQ here:
http://www.eskimo.com/~scs/C-faq/q7.20.html

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

main()
main() should always be explicitly declared as returning int.

{
int i,n,*p;
printf("\n give value for n: ");
scanf("%d", &n);

p= (int *)malloc(n*sizeof(int ));
printf("\n give %d integers: ", n);
for(i=0;i<n;i++){
printf("\nReadig int...");
scanf("%d", (p+i));
printf("\nThe value is %d ", *(p+i));
}
printf("\n the given integers are: ");
Strange convention to always output the newline in the beginning
of the string...

free(p);
p= (int *)malloc(n*sizeof(int ));
Don''t cast the return value of malloc().

for(i=0;i<n;i++)
printf("%d ", *(p+i));




在这里访问未初始化的内存。


干杯,

Andreas

-

Andreas K?h?ri



Accessing uninitialized memory here.

Cheers,
Andreas
--
Andreas K?h?ri


Hassan Iqbal< iq ********** @ extenprise.net>潦草地写着以下内容:
Hassan Iqbal <iq**********@extenprise.net> scribbled the following:

在下面的代码中,我发现即使我已经释放了它,我也能够访问p。
不仅如此在将内存重新分配到p后,甚至可以访问存储在p中的先前值。请有人向我解释一下。
malloc和free在这里做什么?
谢谢,
hassan


访问free()d内存导致未定义的行为。这意味着

实现是免费的(双关语)无论如何都要做任何事情。

它可以做的一些事情是:

1)正常访问内存,就像没有发生任何事情一样。

2)Segfault。

3)损坏HD的主文件系统。

4)打电话给当地警察局并告诉他们你是走私破解。

5)让粉红色蓬松的大象在桌面上跳舞。

你可能是体验行为编号1.对于您的系统,这可能是

,但不能保证适用于所有系统。这正是未定义行为的

点。

#include< stdio.h>
#include< stdlib.h>
#include<文件math.h>
main()
{i / n,n,* p;
printf(\ n给n:的值:);
scanf( %d,& n);
p =(int *)malloc(n * sizeof(int));


因为你有#included< stdlib.h>,放弃演员是安全的。

printf(" \ n给%d整数:,n);
for(i = 0; i< n; i ++){
printf(" \\\
Readig int ...");
scanf( "%d",(p + i));
printf(" \\\
The value is%d",*(p + i));
}
printf( \ n给定的整数是:;);
free(p);
p =(int *)malloc(n * sizeof(int));


您正在为p重新分配更多malloc()ed内存。这意味着

当你使用下面的p时,它几乎没有任何关于内存

你只是免费()d。它只有相同的名称,但内容不同。

for(i = 0; i< n; i ++)
printf("%d",*(p + i) );
//免费(p);
printf(" \ n\ n");
返回0;
}

in the code below i find that i am able to access p even after i have
freed it.
not only that the previous values stored in p are accessible even
after reallocation of memory to p. please some one explain it to me.
what does malloc and free do here?
thanks,
hassan
Accessing free()d memory causes undefined behaviour. This means that
the implementation is free (pun not intended) to do ANYTHING AT ALL.
Some of the things it can do is:
1) Access the memory normally as if nothing had happened.
2) Segfault.
3) Corrupt your HD''s master file system.
4) Call the local police office and tell them you''re smuggling crack.
5) Make pink fluffy elephants dance on your desktop.
You might be experiencing behaviour number 1. This is possible for
your system but not guaranteed for all systems. This is precisely the
point of undefined behaviour.
#include<stdio.h>
#include<stdlib.h>
#include<math.h> main()
{
int i,n,*p;
printf("\n give value for n: ");
scanf("%d", &n); p= (int *)malloc(n*sizeof(int ));
Because you have #included <stdlib.h>, it is safe to drop the cast.
printf("\n give %d integers: ", n);
for(i=0;i<n;i++){
printf("\nReadig int...");
scanf("%d", (p+i));
printf("\nThe value is %d ", *(p+i));
}
printf("\n the given integers are: ");
free(p);
p= (int *)malloc(n*sizeof(int ));
You are reassigning more malloc()ed memory to p. This means that
when you use p below, it has NOTHING AT ALL to do with the memory
you just free()d. It only has the same name, but different contents.
for(i=0;i<n;i++)
printf("%d ", *(p+i));
//free(p);
printf("\n\n");
return 0;
}




你的问题似乎是你为指针值的重新指派分配了某种神秘的意义。你的程序

本质上是一个更复杂的版本:


int main(void){

int p;

p = 1;

p = 2;

返回0;

}


并期望赋值p = 2。失败,因为你试图

指定1 = 2。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)--------------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

众所周知,PC的硬件很棒,但软件很糟糕。

- Petro Tyschtschenko



What your problem seems to be is that you''re assigning some sort of
mystical significance to reassignment of pointer values. Your program
is essentially a more complicated version of:

int main(void) {
int p;
p=1;
p=2;
return 0;
}

and expecting the assignment "p=2" to fail because you''re trying to
assign "1=2".

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"As we all know, the hardware for the PC is great, but the software sucks."
- Petro Tyschtschenko


2003年9月23日星期二,Hassan Iqbal写道:
On Tue, 23 Sep 2003, Hassan Iqbal wrote:

在下面的代码我发现即使在我释放它之后我也能够访问p。
在将内存重新分配给p之后,不仅可以访问存储在p中的先前值。请有人向我解释一下。


调用free()后访问指针是未定义的行为。这个

意味着任何事情都可能发生。它可以格式化你的硬盘驱动器,导致世界和平,鼻子恶魔飞出你的鼻子,或者......如果你从未调用过免费的话,它可以起作用吗? 。关键是,既然你不能保证会发生什么,最好避免未定义的行为。

malloc和free在这里做什么?


它告诉系统它可以破坏指向的数据p。系统

可能什么都不做,直到它需要空闲内存但你不能保证这个.b / b
谢谢,
hassan

#include< stdio.h>
#include< stdlib.h>
#include< math.h>

main()


int main(无效)

{
int i,n,* p;
printf(" \ n给n的值) :);


fflush(stdout);

scanf("%d",& n);


如果用户输入两个,该怎么办?或者哈哈,撞坏你的程序。你应该检查结果。

p =(int *)malloc(n * sizeof(int));


为什么要从malloc投射结果?当你忘记#include< stdlib.h>时,这是一种保证的方式来隐藏
。绝对没有理由

你应该从malloc转换结果。


malloc是否有效?或者是p == NULL?可能想检查一下。

printf(" \ n给%d整数:",n);


fflush(stdout);

for(i = 0; i< n; i ++){
printf(" \\\
Readig int ...");


printf(" \ nReading int ...");

fflush(stdout);

scanf( %d,(p + i));
printf(" \\\
The value is%d",*(p + i));


fflush(stdout);

}
printf(" \ n给定的整数是:");


fflush(stdout);

free(p);
p =(int *)malloc(n * sizeof(int));
for(i = 0; i< n; i ++)
printf("%d",*(p + i));


未定义打印出来的内容。幸运的是你得到了

原始数据。

//免费(p);
printf(" \ n\ n") ;
返回0;
}

in the code below i find that i am able to access p even after i have
freed it.
not only that the previous values stored in p are accessible even
after reallocation of memory to p. please some one explain it to me.
Accessing a pointer after a call to free() is undefined behaviour. This
means that anything can happen. It could format your hard drive, cause
world peace, have nasal demons fly out your nose, or... it could work as
if you never called free(). The point is that since you cannot guarantee
what will happen, it is best to avoid undefined behaviour.
what does malloc and free do here?
It tells the system that it CAN destroy the data p pointed to. The system
will probably do nothing until it needs the free memory but you cannot
guarantee that.
thanks,
hassan

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

main()
int main(void)
{
int i,n,*p;
printf("\n give value for n: ");
fflush(stdout);
scanf("%d", &n);
What if the user inputs "two" or "ha ha, crashed your program." You should
be checking the results.
p= (int *)malloc(n*sizeof(int ));
Why are you casting the result from malloc? This is a guaranteed way to
hide when you forgot to #include <stdlib.h>. There is absolutely no reason
you should be casting the result from malloc.

Did the malloc work? Or is p == NULL? Might want to check that.
printf("\n give %d integers: ", n);
fflush(stdout);
for(i=0;i<n;i++){
printf("\nReadig int...");
printf("\nReading int...");
fflush(stdout);
scanf("%d", (p+i));
printf("\nThe value is %d ", *(p+i));
fflush(stdout);
}
printf("\n the given integers are: ");
fflush(stdout);
free(p);
p= (int *)malloc(n*sizeof(int ));
for(i=0;i<n;i++)
printf("%d ", *(p+i));
Undefined what this is going to print out. It is luck that you got the
original data back.
//free(p);
printf("\n\n");
return 0;
}




-

darrell at cs dot toronto dot edu



main(){int j = 1234; char t [] =":@ abcdefghijklmnopqrstuvwxyz.\ n,* i =

" ; iqgbgxmdbjlgdv.lksrqek.n" ;; char * strchr(const char *,int); while(

* i){j + = strchr(t,* i ++) - t; j%= sizeof t -1; putchar(t [j]);} return 0;}



--
darrell at cs dot toronto dot edu
or
main(){int j=1234;char t[]=":@abcdefghijklmnopqrstuvwxyz.\n",*i=
"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}


这篇关于我可以在免费(p)之后访问'p';的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆