访问释放的内存没有错误 [英] accessing freed memory without error

查看:75
本文介绍了访问释放的内存没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




为什么我在访问释放的内存时没有遇到任何运行时错误

在下面的代码中。这是在标准输出中打印h。


#include< stdio.h>

main()

{

char * buffer =(char *)malloc(6);

strcpy(buffer," hello");

free(buffer);

printf(" buffer =%c\ n",* buffer);

}


-Sachin

Hi,

Why I am not getting any run time error while accessing a freed memory
in following code. This is printing h in std output.

#include<stdio.h>
main()
{
char* buffer = (char*)malloc(6);
strcpy(buffer,"hello");
free(buffer);
printf("buffer=%c\n", *buffer);
}

-Sachin

推荐答案

< sa ******** @ yahoo.com>在消息中写道

news:11 ********************* @ z14g2000cwz.googlegro ups.com ...
<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...


为什么我在访问以下代码中的释放内存时没有遇到任何运行时错误。这是在std输出中打印h。

#include< stdio.h>
main()
{char / buffer =(char *)malloc(6 );
strcpy(缓冲区,你好);
自由(缓冲区);
printf(" buffer =%c \ n",* buffer);
}
Hi,

Why I am not getting any run time error while accessing a freed memory
in following code. This is printing h in std output.

#include<stdio.h>
main()
{
char* buffer = (char*)malloc(6);
strcpy(buffer,"hello");
free(buffer);
printf("buffer=%c\n", *buffer);
}




你已经使用printf进入了未定义行为的领域,并且

任何东西(包括在stdout上打印h)可以发生。根本不做

吧。



You''ve entered the realm of undefined behaviour with the printf, and
anything at all (including printing h on stdout) can happen. Simply don''t do
it.




< sa ******** @ yahoo.com>在消息中写道

news:11 ********************* @ z14g2000cwz.googlegro ups.com ...

<sa********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...


为什么我在访问以下代码中的释放内存时没有遇到任何运行时错误。


C没有任何运行时错误。访问释放(即未分配)的
内存会导致未定义的行为。如果粉红色的兔子会跳出你的b
计算机,那么就标准是

而言,仍然没有任何问题。

这是在标准输出中打印h。

#include< stdio.h>
main()
{* char * buffer =(char *)malloc(6);
strcpy(缓冲区,你好);
free(缓冲区);
printf(" buffer =%c\ n",* buffer);


如果这会打印上一期PlayGirl的内容,那么它也将是

。 }
Hi,

Why I am not getting any run time error while accessing a freed memory
in following code.
C does not have any "run-time-errors". Accessing freed (i.e. non-allocated)
memory results in undefined behavior. If pink rabbits would jump out of your
computer, there would still be nothing wrong as far as the standard is
concerned.
This is printing h in std output.

#include<stdio.h>
main()
{
char* buffer = (char*)malloc(6);
strcpy(buffer,"hello");
free(buffer); printf("buffer=%c\n", *buffer);
If that would print the content of the last issue of PlayGirl, it would be
right, too. }




查看常见问题解答。

http://www.eskimo.com/~scs/C-faq/q7.20.html




蒲公英写道:

dandelion wrote:
C没有任何运行时错误。访问释放(即
未分配)内存会导致未定义的行为。如果粉红色的兔子会跳出你的电脑
,那么就标准的
而言,仍然没有任何问题。
如果那将打印上一期PlayGirl的内容,它
也是对的。
C does not have any "run-time-errors". Accessing freed (i.e. non-allocated) memory results in undefined behavior. If pink rabbits would jump out of your computer, there would still be nothing wrong as far as the standard is concerned.
If that would print the content of the last issue of PlayGirl, it would be right, too.
}




ROTFL。 Pink Rabbits(?),PlayGirl(!)发行......



ROTFL. Pink Rabbits (?), PlayGirl(!) issue...


这篇关于访问释放的内存没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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