与GCC一起运行,与MSVC一起崩溃 [英] Runs with GCC, crashes with MSVC

查看:101
本文介绍了与GCC一起运行,与MSVC一起崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< deque>

#include< iostream>

使用命名空间std;


int main( )

{

deque< int> foo;

foo.push_back(42);

deque< int> :: reverse_iterator it = foo.rbegin();

while(它!= foo.rend())

{

cout<< start of loop \ n;;

int temp = * it ++;

foo.pop_back();

}

返回0;

}


使用GCC 3.3.1编译时,上面的程序打印

" ;开始循环一次并且优雅地退出(因为我会希望它能做到这一点)。


当用MSVC 6(SP6)编译时,它会显示start of

loop"两次崩溃。


谁是对的?

#include <deque>
#include <iostream>
using namespace std;

int main()
{
deque<int> foo;
foo.push_back(42);
deque<int>::reverse_iterator it = foo.rbegin();
while (it != foo.rend())
{
cout << "start of loop\n";
int temp = *it++;
foo.pop_back();
}
return 0;
}

When compiled with GCC 3.3.1, the program above prints
"start of loop" once and exits gracefully (as I would
expect it to do).

When compiled with MSVC 6 (SP6), it displays "start of
loop" twice and crashes.

Who is right?

推荐答案

Derek写道:
#include< deque>
#include< iostream>
使用命名空间std;

int main()
{
deque< int> ; foo;
foo.push_back(42);
deque< int> :: reverse_iterator it = foo.rbegin();
while(it!= foo.rend())
{
cout<< start of loop \\\
;
int temp = * it ++;
foo.pop_back();


pop_back使迭代器无效指向弹出元素的内容。

在你的情况下,因为''它'是一个反向迭代器,它真的指向

元素在取消引用后的元素之后。我想,它只是

迭代器实现技巧之一。所以,当你说
说它= foo.rbegin()时,它基本上与它= foo.end()相同,但是

有一个扭曲。现在,它将它移动到指向最后一个元素,即你在下一个语句中迅速弹出的


之后迭代器无效。

}
返回0;


使用GCC 3.3编译时。如图1所示,上面的程序打印出开始循环。一次并优雅地退出(正如我希望的那样)。

当使用MSVC 6(SP6)编译时,它会显示
循环开始。两次崩溃。

谁是对的?
#include <deque>
#include <iostream>
using namespace std;

int main()
{
deque<int> foo;
foo.push_back(42);
deque<int>::reverse_iterator it = foo.rbegin();
while (it != foo.rend())
{
cout << "start of loop\n";
int temp = *it++;
foo.pop_back();
pop_back invalidates the iterator what pointed to the popped element.
In your case, since ''it'' is a reverse iterator, it really points to
the element right after the one it gives when dereferenced. It''s just
one of the iterator implementation tricks, I suppose. So, when you
say it = foo.rbegin(), it basically the same as it = foo.end(), but
with a twist. Now, it++ moves it to point to the last element, which
you promptly pop in the next statement. The iterator is invalid after
that.
}
return 0;
}

When compiled with GCC 3.3.1, the program above prints
"start of loop" once and exits gracefully (as I would
expect it to do).

When compiled with MSVC 6 (SP6), it displays "start of
loop" twice and crashes.

Who is right?




两者都是对的。你正在尝试使用无效的迭代器,

因此得到未定义的行为。


你可能应该做的


while(!foo.empty())

{

cout<< start of loop \\\
;

int temp = * foo.rbegin(); //或int temp = foo.back();

foo.pop_back();

}


Victor



Both are right. You''re trying to make use of an invalid iterator,
and therefore get undefined behaviour.

You probably should have done

while (!foo.empty())
{
cout << "start of loop\n;
int temp = *foo.rbegin(); // or int temp = foo.back();
foo.pop_back();
}

Victor


Derek< us ** @ nospam.org>在新闻中写道:2l ************ @ uni-berlin.de:
Derek <us**@nospam.org> wrote in news:2l************@uni-berlin.de:
#include< deque>
#include< iostream>
使用命名空间std;

int main()
{deque< int> foo;
foo.push_back(42);
deque< int> :: reverse_iterator it = foo.rbegin();
while(it!= foo.rend())
{
cout<< start of loop\\\
;
int temp = * it ++;
foo.pop_back();
}
返回0;
}

使用GCC 3.3.1编译时,上面的程序打印出循环开始。一次并优雅地退出(正如我希望的那样)。

当使用MSVC 6(SP6)编译时,它会显示
循环开始。两次和崩溃。

谁是对的?
#include <deque>
#include <iostream>
using namespace std;

int main()
{
deque<int> foo;
foo.push_back(42);
deque<int>::reverse_iterator it = foo.rbegin();
while (it != foo.rend())
{
cout << "start of loop\n";
int temp = *it++;
foo.pop_back();
}
return 0;
}

When compiled with GCC 3.3.1, the program above prints
"start of loop" once and exits gracefully (as I would
expect it to do).

When compiled with MSVC 6 (SP6), it displays "start of
loop" twice and crashes.

Who is right?




两者:)根据Josuttis的说法,你正在处理一个无效的

iterator。


"因此,任何插入或删除都会使所有指针,引用,

和引用其他元素的迭代器无效。双端队列。例外

是在前面或后面插入元素的时候。



Both :) According to Josuttis, you''re working with an invalidated
iterator.

"Thus, any insertion or deletion invalidates all pointers, references,
and iterators that refer to other elements of the deque. The exception
is when elements are inserted at the front or the back."


Andre Kostur写道:
Andre Kostur wrote:
Derek< us ** @ nospam.org>在新闻中写道:2l ************ @ uni-berlin.de:

Derek <us**@nospam.org> wrote in news:2l************@uni-berlin.de:

#include< deque>
#include< iostream>
使用命名空间std;

int main()
{deque< int> foo;
foo.push_back(42);
deque< int> :: reverse_iterator it = foo.rbegin();
while(it!= foo.rend())
{
cout<< start of loop\\\
;
int temp = * it ++;
foo.pop_back();
}
返回0;
}

使用GCC 3.3.1编译时,上面的程序打印出循环开始。一次并优雅地退出(正如我希望的那样)。

当使用MSVC 6(SP6)编译时,它会显示
循环开始。两次崩溃。

谁是对的?
#include <deque>
#include <iostream>
using namespace std;

int main()
{
deque<int> foo;
foo.push_back(42);
deque<int>::reverse_iterator it = foo.rbegin();
while (it != foo.rend())
{
cout << "start of loop\n";
int temp = *it++;
foo.pop_back();
}
return 0;
}

When compiled with GCC 3.3.1, the program above prints
"start of loop" once and exits gracefully (as I would
expect it to do).

When compiled with MSVC 6 (SP6), it displays "start of
loop" twice and crashes.

Who is right?



两者:)根据Josuttis的说法,你正在使用一个无效的
迭代器。<因此,任何插入或删除都会使引用deque的其他元素的所有指针,引用,和/和迭代器无效。例外
是在前面或后面插入元素的时候。


Both :) According to Josuttis, you''re working with an invalidated
iterator.

"Thus, any insertion or deletion invalidates all pointers, references,
and iterators that refer to other elements of the deque. The exception
is when elements are inserted at the front or the back."




实际上从正面或背面弹出,根据

标准,仅使指向那些对象的迭代器无效。

问题是,反向迭代器确实指向对象一个

过去了你认为他们指的是什么。


当然,Josuttis的规则有点严格,所以如果你遵守它,

你不太可能但是,如果犯了错误,你会受到更多的限制(..,我认为那''严格'是什么意思,更多的限制,

对吗?...)


Victor



Actually popping from the front or from the back, according to the
Standard, invalidates only the iterators that point to those objects.
The trouble is, the reverse iterators really point to the object one
past the one you think they point to.

Of course, Josuttis'' rule is a bit more strict, so if you follow it,
you are less likely to make a mistake, although, you''d be restricted
more (..yeah, I suppose that''s what ''strict'' means, more restrictions,
right?...)

Victor


这篇关于与GCC一起运行,与MSVC一起崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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