且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

访问释放的内存没有错误

更新时间:2023-02-02 07:58:46

< 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&GT;在消息中写道

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...