本文介绍了该代码是有效的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void st(const char * a)

{

cout<< a;

删除(a);

}


int main()

{

char foo [] =" CPP";


st(foo);

cout<< " \\\
\\\
" << foo;


系统(PAUSE);

返回0;

}


特别是在函数st中我使用删除(a)


也是删除foo不仅仅是一个

void st(const char *a)
{
cout << a;
delete(a);
}

int main ()
{
char foo[]= "CPP";

st(foo);
cout << "\n\n" << foo;

system("PAUSE");
return 0;
}

particular in the function st when i use delete(a)

is that also delete foo not only a

推荐答案



代码无效。使用delete仅用于分配new的内存。

删除其他内容会导致崩溃。


如果有什么你不明白尝试询问一个关于它的问题

而不是显示一些疯狂的混合代码。


-

Scott McPhillips [MVP VC ++ ]

The code is not valid. Use delete only for memory allocated with new.
Deleting something else will lead to a crash.

If there''s something you don''t understand try asking a question about it
instead of showing some crazy mixed up code.

--
Scott McPhillips [MVP VC++]




代码无效。


The code is not valid.



更确切地说:它有未定义的行为。

To be more precise: It has undefined behavior.




昨天,Jim Langston告诉你:


删除只应该在分配有new的内存上使用,使用delete []

with new []

如果你不想听听别人的话,那就不要浪费时间了。

每个人的时间。


Brian

Yesterday, Jim Langston told you:

"delete should only be used on memory allocated with new, use delete[]
with new[]"
If you aren''t going to LISTEN to what people you, then stop wasting
everyone''s time.

Brian


这篇关于该代码是有效的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 18:11
查看更多