调试运行时出现奇怪的崩溃程序(Eclipse C++)

Weird crashing program while debug runs smoothly (Eclipse C++)

本文关键字:程序 Eclipse C++ 崩溃 运行时 调试      更新时间:2023-10-16

我正在为我的大学算法数学课编写一个程序,我正在使用Win 7(x64(,Eclipse Oxygen.1a版本(4.7.1a(和MinGW 6.3.0。

每当我构建并运行程序时,它都会崩溃,窗口声称"Abgabe3.exe停止工作",但是当尝试使用调试器和断点查找问题时,我逐步执行整个程序,它完成没有错误...

我剥离了有问题的功能未使用的所有内容,并将所有内容复制到一个单独的文件中,然后出现了确切的问题。也许有人知道我身边发生了什么。^^

#include <math.h>       /* pow, sqrt */
#include <iostream>     /* cin, cout */
#include <new>          /* new */
#include <string>       /* string */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
using namespace std;

void NORM(double* res, double* x, int n){
    res[0] = 0.0;
    for(int i = 0; i < n; i++){
        res[0] += pow(x[i], 2);
    }
    res[0] = sqrt(res[0]);
}
void initRand(double* x, int n){
    srand (time(NULL) * rand());
    for(int i = 0; i < n; i++){
        x[i] = (((double) rand()) / ((double) RAND_MAX));
    }
}
void createArray(double* &x, int n){
    if (n > 0){
        x = new double[n];
        initRand(x, n);
    }
}
void printArray(double* x, int n){
    if (x != NULL){
    cout<<"(n";
    for(int i = 0; i < n; i++){
        if(i+1 == n) cout<<x[i];
        else if ((i % 5) == 0) cout<<x[i];
        else if ( ((i+1) % 5) == 0 ){
            cout<<", "<<x[i]<<"n";
        }
        else {
            cout<<", "<<x[i];
        }
    }
    cout<<"n)n";
    }
    else cout<<"nError: pointer = NULLn";
}
unsigned long long int bin(unsigned int n, unsigned int k){
    unsigned long long res = 1;
    if(k == 0) return 1;
    else if( n >= k){
        for(unsigned long long int i = 1; i <= k; i++){
            res *= (n + 1 - i) / i;
        }
    }
    else return 0;
    return res;
}
void newArray(double** x, unsigned int v, unsigned int n){
    for(unsigned int i = 0; i < v; i++){
        double* ptr = x[i];
        createArray(ptr,n);
        x[i] = ptr;
    }
}
void experiment(double** vektorArray){
    unsigned int n = 10, v = 20;
    cout<<"Dimension n = "<<n<<"nAnzahl Versuche v = "<<v<<endl;
    //Erstellen der Vektoren
    cout<<"Erstellen - starten";
    vektorArray = new double*[n];
    newArray(vektorArray, v, n);
    cout<<"Erstellen - fertign";
    for(unsigned int i = 0; i < v; i++){
        if(i%10 == 0) printArray(vektorArray[i], n);
    }
}
int main(int argc, char** argv){
    double** vektorArray = NULL;
    experiment(vektorArray);
    return 0;
}
vektorArray = new double*[n];

创建了一个大小为 n 的数组,但是

void newArray(double** x, unsigned int v, unsigned int n)
{
    for (unsigned int i = 0; i < v; i++)
    {
        double* ptr = x[i];
        createArray(ptr, n);
        x[i] = ptr;
    }
}

for (unsigned int i = 0; i < v; i++)
{
    if (i % 10 == 0)
        printArray(vektorArray[i], n);
}

v 索引该数组。看起来你交叉了你的变量。强烈建议为变量提供更好、更具描述性的名称,以帮助使其更明显。