奇怪的输出,帮助。 [英] Strange Output, Help.

查看:65
本文介绍了奇怪的输出,帮助。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题与海湾合作委员会有关。但我希望有人在这里

可以告诉我一些关于原因的详细信息。

我正在使用的g ++版本是:g ++(GCC)3.4.4(mingw特价)。


代码为:

// BEGIN

#include< iostream>

int main(无效)

{

int * pI1 = new int;

int * pI2 = new int;

int * pI3 = new int;

int * pI4 = new int;

int * pI5 = new int;

int * pI6 = new int;

int * pI7 = new int;


* pI1 = 43;

* pI2 = 56;

* pI3 = 78;

* pI4 = 98;

* pI5 = 99;

* pI6 = 123;

* pI7 = 123;


std :: cout<< * pI1<< std :: endl;

std :: cout<< * pI2<< std :: endl;

std :: cout<< * pI3<< std :: endl;

std :: cout<< * pI4<< std :: endl;

std :: cout<< * pI5<< std :: endl;

std :: cout<< * pI6<< std :: endl;

std :: cout<< * pI7<< std :: endl;


删除pI1;

删除pI2;

删除pI3;

删除pI4;

删除pI5;

删除pI6;

删除pI7;


std: :cout<< * pI1<< std :: endl;

std :: cout<< * pI2<< std :: endl;

std :: cout<< * pI3<< std :: endl;

std :: cout<< * pI4<< std :: endl;

std :: cout<< * pI5<< std :: endl;

std :: cout<< * pI6<< std :: endl;

std :: cout<< * pI7<< std :: endl;


返回0;

} //结束


输出为:

43

56

78

98

99

123

123

0

4013536

4013640

3998472
123

123

I know that this problem is concerned to GCC. But I hope somebody here
can tell me some detailed things about why.
The version of g++ I''m using is: g++ (GCC) 3.4.4 (mingw special).

The code is:
// BEGIN
#include<iostream>

int main(void)
{
int *pI1 = new int;
int *pI2 = new int;
int *pI3 = new int;
int *pI4 = new int;
int *pI5 = new int;
int *pI6 = new int;
int *pI7 = new int;

*pI1 = 43;
*pI2 = 56;
*pI3 = 78;
*pI4 = 98;
*pI5 = 99;
*pI6 = 123;
*pI7 = 123;

std::cout << *pI1 << std::endl;
std::cout << *pI2 << std::endl;
std::cout << *pI3 << std::endl;
std::cout << *pI4 << std::endl;
std::cout << *pI5 << std::endl;
std::cout << *pI6 << std::endl;
std::cout << *pI7 << std::endl;

delete pI1;
delete pI2;
delete pI3;
delete pI4;
delete pI5;
delete pI6;
delete pI7;

std::cout << *pI1 << std::endl;
std::cout << *pI2 << std::endl;
std::cout << *pI3 << std::endl;
std::cout << *pI4 << std::endl;
std::cout << *pI5 << std::endl;
std::cout << *pI6 << std::endl;
std::cout << *pI7 << std::endl;

return 0;
} // END

The output is:
43
56
78
98
99
123
123
0
4013536
4013640
3998472
123
123

推荐答案

删除后,第一个值(* pI1 )没关系。但其他人

让我很困惑。

我也在VC6.0下编译了它。结果是正确。

我确定这是编译器特定的。我只是想知道GCC中的g ++

如何对待这个。

我也尝试过:

// BEGIN

char * str = new char [0];

str =" gaga";

std :: cout<< * str<< std :: endl

<< str<< std :: endl;

delete [] str;

std :: cout<< * str<< std :: endl

<< str<< std :: endl;

// END

输出也很困惑!

After being deleted, the first value(*pI1) is ok. But the others
puzzled me a lot.
I''ve also compiled it under VC6.0. The result is "right".
I''m sure this is compiler specific. I just want to know how g++ in GCC
treat this.
I also tried:
// BEGIN
char *str = new char[0];
str = "gaga";
std::cout << *str << std::endl
<< str << std::endl;
delete [] str;
std::cout << *str << std::endl
<< str << std::endl;
// END
The output is puzzled too!


On 2005年12月29日星期四17:46:52 -0800,weichaoliu写道:
On Thu, 29 Dec 2005 17:46:52 -0800, weichaoliu wrote:
我知道这个问题与GCC有关。但我希望有人能够告诉我一些关于原因的详细信息。
我正在使用的g ++版本是:g ++(GCC)3.4.4(mingw special)。
I know that this problem is concerned to GCC. But I hope somebody here
can tell me some detailed things about why.
The version of g++ I''m using is: g++ (GCC) 3.4.4 (mingw special).




< snip of offnding code>


这个问题并不是GCC特有的。问题出在代码中;它们被删除后取消引用指针是

。这是未定义的

行为,任何事情都可能发生(包括预期的

结果)。


修复就是不要这样做。


- Jay



<snip offending code>

The problem isn''t specific to GCC. The problem is in the code; it is
dereferencing pointers after they''ve been deleted. This is Undefined
Behavior, and anything can happen as a result (including "expected"
results).

The fix is Don''t Do That.

- Jay


2005年12月29日星期四17: 55:45 -0800,weichaoliu写道:
On Thu, 29 Dec 2005 17:55:45 -0800, weichaoliu wrote:
删除后,第一个值(* pI1)就可以了。但是其他人很困惑我。
我也在VC6.0下编译了它。结果是正确。
我确定这是编译器特定的。我只是想知道GCC中g ++如何对待这个。
我也尝试过:
//开始
char * str = new char [0];
str =" gaga";
std :: cout<< * str<< std :: endl
<< str<< std :: endl;
delete [] str;
std :: cout<< * str<< std :: endl
<< str<< std :: endl;
// END
输出也很困惑!
After being deleted, the first value(*pI1) is ok. But the others
puzzled me a lot.
I''ve also compiled it under VC6.0. The result is "right".
I''m sure this is compiler specific. I just want to know how g++ in GCC
treat this.
I also tried:
// BEGIN
char *str = new char[0];
str = "gaga";
std::cout << *str << std::endl
<< str << std::endl;
delete [] str;
std::cout << *str << std::endl
<< str << std::endl;
// END
The output is puzzled too!




一旦你将内存返回堆中,你就不再拥有了它。内存

分配例程可以随心所欲地执行任何操作。


同样,根据标准没有定义行为,也没有任何好处可以来/>
这样做。


说不。 :)


- 周杰伦



Once you return memory to the heap, you no longer own it. The memory
allocation routines can do whatever they want with it.

Again, the behavior is not defined per the standard, and no good can come
of doing it.

Just say no. :)

- Jay


这篇关于奇怪的输出,帮助。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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