简体   繁体   English

这个循环不断给我细分错误的任何解决方法?

[英]This loop keeps giving me segmentation faults any fixes?

The purpose of the loop is to go through two stacks that are being compared at the same time, and placed into temporary stacks(c and d) while the rest is being popped, however this gives me segmentation faults. 循环的目的是同时比较两个要比较的堆栈,并在弹出其余堆栈时将它们放入临时堆栈(c和d),但这给我带来了分段错误。 I am still learning c++ and segfaults have been my biggest problem. 我仍在学习c ++,segfaults是我最大的问题。

    while((a.top()==b.top())&&(!a.empty())&&(!b.empty())){
        e=a.top();
        f=b.top();
        a.pop();
        b.pop();
        c.push(e);
        d.push(f);
    }

EDIT: a and b are two stacks that have been previously defined and are to be compared to each other. 编辑:a和b是两个堆栈,之前已定义并且要相互比较。

c and d are the temporary stacks that will hold the data of a and b as they are popped out, c和d是临时堆栈,将在弹出时保存a和b的数据,

e and f are value placeholders for the item inside of a and b to be placed in c and d. e和f是要放置在c和d中的a和b内的项目的值占位符。

while(!(a.empty() || b.empty())
      && (a.top() == b.top()))
{
        e=a.top();
        f=b.top();
        a.pop();
        b.pop();
        c.push(e);
        d.push(f);
}

note the order of your tests. 注意测试的顺序。 your code will exhibit undefined behaviour if either container is empty. 如果任一容器为空,则您的代码将显示未定义的行为。 The order of the tests matter because the evaluation of the second operand of && will not be evaluated if the first evaluates to false. 测试的顺序很重要,因为如果&&的第二个操作数的评估结果为false,则不会对其进行评估。

edit: 编辑:

I was a little bothered by the number of copies going in in the loop (yes, I too am guilty of looking for ways to prematurely optimise a program :-) ). 我对循环中的拷贝数感到不安(是的,我也很内looking地寻找过早优化程序的方法:-))。

Depending on how many matching items are on your stacks, you might get better performance like this (c++11 or better): 根据堆栈中有多少个匹配项,您可能会获得更好的性能(c ++ 11或更高):

while(!(a.empty() || b.empty())
      && (a.top() == b.top()))
{
    c.push(std::move(a.top()));  // move the top item of a to the top of c
    a.pop();                     // destroy the shrivelled husk of what used to be 
                                 // at the top of a.

    d.push(std::move(b.top()));
    b.pop();
}

Hard to say without mode code but assuming these are std::stack objects, you might want to check they are not empty before calling top on them Change 没有模式代码很难说,但是假设它们是std::stack对象,则可能需要调用top 之前检查它们是否为空

 while((a.top()==b.top())&&(!a.empty())&&(!b.empty())){

to

while((!a.empty())&&(!b.empty())&&(a.top()==b.top())){

top returns a reference but if there is nothing on the stack then what would that be a reference to?? top返回一个引用,但是如果堆栈上没有任何内容,那将是什么引用? This would lead to undefined behaviour. 这将导致不确定的行为。 There is no point checking for emptiness after calling top . 调用top 之后,没有任何检查是否为空的步骤。 By changing the order in this way, you are certain to not call top on either a or b if one (or both) are empty. 通过以这种方式更改顺序,可以确定如果一个(或两个)为空,则不要在ab上调用top

Assuming that the information in this link http://www.cplusplus.com/reference/stack/stack/top/ is correct, then it suggests that top calls back on the underlying container and this will certainly result in undefined behaviour for some (if not all) containers when they are empty. 假设此链接http://www.cplusplus.com/reference/stack/stack/top/中的信息是正确的,则表明top back了基础容器,这肯定会导致某些行为的不确定( (如果不是全部)容器,则它们为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM