我有一些由Assembly模块调用的C ++代码。我的代码应该通过指针对数组进行排序。
我的两个问题存在于第二个for-loop
:
for-loop
时,我会立即注释掉cout和程序段错误,而不是第一个只输出数组的程序。我能够看到第一个for-loop
。for-loop
运行进行注释,然后在必须增加i
时运行段错误。我能够看到for-loop
所做的所有输出。以下是代码:
#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;
}
答案 0 :(得分:3)
int j = i+1; i <= size; j++;
我认为这导致了这个问题。