【问题标题】:Runtime error in c++ when analysing time [closed]分析时间时c ++中的运行时错误[关闭]
【发布时间】:2016-07-31 17:14:23
【问题描述】:

我正在尝试检查每个 3 个解决方案的问题经过多长时间,但有时我会遇到运行时错误并且看不到第 3 个解决方案的经过时间,但有时它可以工作。我认为solutions.h 文件是正确的,但我还是把它放在这里。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "solutions.h"
using namespace std;

    int main()
{
cout << "Hello world!" << endl;
int* input1 = new int[10000];
int* input2 = new int[20000];
int* input3 = new int[40000];
int* input4 = new int[80000];
int* input5 = new int[100000];

for(int i = 0; i<100000; i++)
{
    input1[i]= rand();
    input2[i]= rand();
    input3[i]= rand();
    input4[i]= rand();
    input5[i]= rand();
}
int* output1= new int[1000];

double duration;


clock_t startTime1 = clock();
solution1(input1,10000,1000,output1);
duration = 1000 * double( clock() - startTime1 ) / CLOCKS_PER_SEC;
cout << "Solution 1 with 10000 inputs took " << duration << " milliseconds." << endl;

startTime1 = clock();
solution2(input1,10000,1000,output1);
duration = 1000 * double( clock() - startTime1 ) / CLOCKS_PER_SEC;
cout << "Solution 2 with 10000 inputs took " << duration<< " milliseconds." << endl;

startTime1 = clock();
solution3(input1,10000,1000,output1);
duration = 1000 * double( clock() - startTime1 ) / CLOCKS_PER_SEC;
cout << "Solution 3 with 10000 inputs took " << duration << " milliseconds." << endl<<endl<<endl;



return 0;
}

solutions.h 是

#ifndef SOLUTIONS_H_INCLUDED
#define SOLUTIONS_H_INCLUDED
#include <cmath>

void solution1( int input[], const int n, const int k, int output[] );
void solution2( int input[], const int n, const int k, int output[] );
void solution3( int input[], const int n, const int k, int output[] );

void swap( int &n1, int &n2 ) {

int temp = n1;
n1 = n2;
n2 = temp;
}

void solution1( int input[], const int n, const int k, int output[] ) {

int maxIndex, maxValue;
for( int i = 0; i < k; i++ ) {
    maxIndex = i;
    maxValue = input[i];
    for( int j = i+1; j < n; j++ ) {
        if( input[j] >= maxValue ) {
            maxIndex = j;
            maxValue = input[ j ];
        }
    }
    swap( input[i], input[maxIndex] );
    output[i] = input[i];
}
}

int partition( int input[], int p, int r ) {

int x = input[ r ], i = p - 1;
for( int j = p; j < r; j++ ) {
    if( input[ j ] >= x ) {
        i = i + 1;
        swap( input[i], input[j] );
    }
}
swap( input[i+1], input[r] );
return i + 1;
}

void quickSort( int input[], int p, int r ) {

int q;
if( p < r ) {
    q = partition( input, p, r );
    quickSort( input, p, q - 1 );
    quickSort( input, q + 1, r );
}
}

void solution2( int input[], const int n, const int k, int output[] ) {

quickSort( input, 0, n - 1 );
for( int i = 0; i < k; i++ ) {
    output[i] = input[i];
}
}

int partition2( int input[], int a, int p, int r ) {

int x = a, i = p - 1;
for( int j = p; j < r; j++ ) {
    if( input[ j ] == x ) {
        swap( input[ j ], input[ r ] );
    }
    if( input[ j ] >= x ) {
        i = i + 1;
        swap( input[i], input[j] );
    }
}
swap( input[ i + 1 ], input[ r ] );
return i + 1;
}

void quickSort2( int input[], int p, int r ) {

int q;
if( p < r ) {
    q = partition2( input, input[ r ], p, r );
    quickSort2( input, p, q - 1 );
    quickSort2( input, q + 1, r );
}
}

int findMin( int n1, int n2 ) {

if( n1 <= n2 )
    return n1;
else
    return n2;
}

int select( int input[], int n, int k, int start, int end, int flag ) {

if( n <= 5 ) {
    quickSort2( input, start, end );
    return input[ start + k - 1 ];
}
int i = start, numGroups = (int) ceil( ( double ) n / 5 ), numElements, j =     0;
int *medians = new int[numGroups];
while( i <= end ) {
    numElements = findMin( 5, end - i + 1 );
    medians[( i - start ) / 5] = select( input, numElements, (int) ceil( (   double ) numElements / 2 ), i, i + numElements - 1, 1 );
    i = i + 5;
}
int M = select( medians, numGroups, (int) ceil( ( double ) numGroups / 2 ), 0, numGroups - 1, 1 );
delete[] medians;
if( flag == 1 )
    return M;
int q = partition2( input, M, start, end );
int m = q - start + 1;
if( k == m )
    return M;
else if( k < m )
    return select( input, m - 1, k, start, q - 1, 0 );
else
    return select( input, end - q, k - m, q + 1, end, 0 );
 }

 void solution3( int input[], const int n, const int k, int output[] ) {

select( input, n, k, 0, n - 1, 0 );
for( int i = 0; i < k; i++ )
    output[i] = input[i];
 }



#endif // SOLUTIONS_H_INCLUDED

【问题讨论】:

  • 你得到的运行时错误是什么?
  • 进程返回-1073741819 (0xC0000005)
  • 可能是溢出。初始化输入数组时至少有一个,例如。 input1 的大小为 10000,您正尝试将 100000 个元素放入其中。这个问题的代码太大了。请缩小范围。

标签: c++ runtime analysis timing ctime


【解决方案1】:

使用地址清理程序 (clang++ clock.cxx -std=c++11 -O1 -g -fsanitize=address -fno-omit-frame-pointer) 构建程序会发现问题:

$ ./a.out 
Hello world!
=================================================================
==8175==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x62e00000a040 at pc 0x000104dbd912 bp 0x7fff5ae43970 sp 0x7fff5ae43968
WRITE of size 4 at 0x62e00000a040 thread T0
    #0 0x104dbd911 in main clock.cxx:18
    #1 0x7fff88cd85fc in start (libdyld.dylib+0x35fc)
    #2 0x0  (<unknown module>)

0x62e00000a040 is located 0 bytes to the right of 40000-byte region [0x62e000000400,0x62e00000a040)

还有你的代码:

  int* input1 = new int[10000];
  int* input2 = new int[20000];
  int* input3 = new int[40000];
  int* input4 = new int[80000];
  int* input5 = new int[100000];

  for(int i = 0; i<100000; i++)
    {
      input1[i]= rand();
      input2[i]= rand();
      input3[i]= rand();
      input4[i]= rand();
      input5[i]= rand();
    }

如您所见,input1、input2、...、input4 的大小为 10K、20K、40K、80K 元素,但在循环中,我们正在访问此数组之外的元素,因此这可能导致堆损坏.

进程返回-1073741819 (0xC0000005)

这意味着“内存访问冲突”或 SEGFAULT。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多