简体   繁体   English

到达for循环时出现奇怪的分段错误

[英]Strange segmentation fault when reaching for-loop

I have some C++ code that is being called by an Assembly module. 我有一些由Assembly模块调用的C ++代码。 My code is supposed to sort an array by pointers. 我的代码应该按指针对数组进行排序。

My two issues reside in the second for-loop : 我的两个问题位于第二个for-loop

  • I comment out the couts and the program immediately segfaults when it approaches the second for-loop in my code, not the first one that just outputs the array. 我注释掉了提示,当程序接近我的代码中的第二个for-loop ,而不是仅输出数组的第一个for-loop时,程序立即出现了段错误。 I am able to see the output of the first for-loop . 我能够看到第一个for-loop的输出。
  • I do not comment out the couts and the for-loop runs then segfaults when it has to increment i . 我没有注释掉cout, for-loop运行,然后在segfaults必须增加i时运行。 I am able to see all the outputs this for-loop makes. 我能够看到此for-loop产生的所有输出。

Here's the code: 这是代码:

#include <iostream>

using namespace std;

extern "C" void swapASM(double (**address));

extern "C" void sortbypointers(double *arr[], long size)
{
    cout << "Before sort: " << endl;
    for(int i = 0; i < size; i++)
    {
        cout << (*(arr[i])) << " ";
    }

    // problems starts here
    for(int i = 0; i < size; i++)
    {
        cout << endl << "i: " << i;
        for(int j = i+1; i <= size; j++)
        {
            if((*(arr[i])) > (*(arr[j])))
            {
                //cout << endl << "Before swap: \ni: " << i << "\narr[i]: " << *(arr[i]) << "\nj: " << j << "\narr[j]: " << *(arr[j]) << endl;
                swapASM(&(arr[i]));
                //cout << endl << "After swap: \ni: " << i << "\narr[i]: " << *(arr[i]) << "\nj: " << j << "\narr[j]: " << *(arr[j]) << endl;
            }
        }
    }

    cout << endl << "After sort: " << endl;
    for(int i = 0; i < size; i++)
    {
        cout << *(arr[i]) << " ";
    }

    cout << endl << "Return" << endl;
    return;

}
int j = i+1; i <= size; j++;

我认为这是造成问题的原因。

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

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