【问题标题】:How do I resolve "Cannot Access Memory at Address c++" issue如何解决“无法在地址 c++ 处访问内存”问题
【发布时间】:2019-07-06 09:40:16
【问题描述】:

我从 Eclipse IDE 收到以下调试错误

执行 MI 命令失败:
-data-evaluate-expression *((array500000)+30000)@10000
来自调试器后端的错误消息:
无法访问地址 0x42f250 的内存

但我不确定如何解决该问题。有人能给我任何建议吗?我是编码新手。

尝试了不同的 IDE。我的代码适用于在线 IDE,但不适用于其他 IDE。

using namespace std;
using namespace std::chrono;

void Swap(int *x,int *y) {
int temp=*x;
*x=*y;
*y=temp;
}

void Insertion(int A[],int n) {
    int i,j,x;
    for(i=1;i<n;i++) {
        j=i-1;
        x=A[i];
        while(j>-1 && A[j]>x){
            A[j+1]=A[j];
            j--;
        }
        A[j+1]=x;
    }
}

void Merge(int A[],int l,int mid,int h) {
    int i=l,j=mid+1,k=l;
    int B [500000];
    while(i<=mid && j<=h) {
        if(A[i]<A[j])
            B[k++]=A[i++];
        else
            B[k++]=A[j++];
    }
    for(;i<=mid;i++) {
        B[k++]=A[i];
    }
    for(;j<=h;j++) {
        B[k++]=A[j];
    }
    for(i=l;i<=h;i++) {
        A[i]=B[i];
    }
}

void MergeSort(int A[],int l,int h) {
    if(l<h) {
        int mid=(l+h)/2;
        MergeSort(A,l,mid);
        MergeSort(A,mid+1,h);
        Merge(A,l,mid,h);
    }
}

void ArrayAscend (int A[], int n) {
    for (int a = 0; a < n ; a++) {
        A [a] = a + 1;
    }
}

void ArrayRandom (int A[], int n) {
       ArrayAscend (A,n);
       srand (time(NULL));
       for (int  i= n-1 ; i > 0; i--) {
              int j = rand () % (i+1);
              Swap (&A[i], &A[j]);
       }
}

void ArrayDescend (int A[], int n) {
       for (int a = 0; a < n ; a++) {
        A [a] = n - a;
    }
} 

int main() {


   int arraySize500000 = 500000;
   int array500000[arraySize500000] = {};


  cout << "*********************Insertion Sort*********************" <<endl;

  cout << "---------- Arrays with 500000 Integers ----------" <<endl;

  ArrayAscend (array500000,arraySize500000);
  auto t1 = system_clock::now();
  Insertion(array500000,arraySize500000);
  auto t2 = system_clock::now();
  auto duration1 = duration_cast<microseconds>(t2-t1);
  cout << "Array in ascending order took " << duration1.count()<<" microseconds"<<endl;

  ArrayDescend (array500000,arraySize500000);
  auto t3 = system_clock::now();
  Insertion(array500000,arraySize500000);
  auto t4 = system_clock::now();
  auto duration2 = duration_cast<microseconds>(t4-t3);
  cout << "Array in descending order took " << duration2.count()<<" microseconds"<<endl;

  ArrayRandom (array500000,arraySize500000);
  auto t5 = system_clock::now();
  Insertion(array500000,arraySize500000);
  auto t6 = system_clock::now();
  auto duration3 = duration_cast<microseconds>(t6-t5);
  cout << "Array in random order took " << duration3.count()<<" microseconds"<<endl;

return 0;
}

我期望每个插入排序的持续时间的输出。

【问题讨论】:

  • 好吧,我猜是 int arraySize500000 = 500000; int array500000[arraySize500000] = {}; 不是合法的 C++(因为数组边界必须是编译时间常数)。尝试以下版本constexpr int arraySize500000 = 500000; int array500000[arraySize500000] = {};。添加 constexpr 会将您的代码转换为合法的 C++。

标签: c++ debugging memory-leaks


【解决方案1】:
int B [500000];
int arraySize500000 = 500000;
int array500000[arraySize500000] = {};

即使这被转换为 constexpr,正如 John 所指出的,这些也会在堆栈上创建 2MB 的数组。例如,Visual Studio 仅分配了 1MB of stack

您应该改用std::vector&lt;int&gt; array500000(500000)

【讨论】:

    猜你喜欢
    • 2018-09-08
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    相关资源
    最近更新 更多