获取退出值:-1,073,741,571 用简单的代码计算第 n 时刻

Posted

技术标签:

【中文标题】获取退出值:-1,073,741,571 用简单的代码计算第 n 时刻【英文标题】:Getting exit value: -1,073,741,571 with simple code to calculate nth moment 【发布时间】:2020-05-03 20:46:30 【问题描述】:

我在计算随机数数组的第 n 时刻(如质心,将是第 1 时刻)时遇到问题。我在eclipse中用C编码,当我尝试用gcc编译时也会出现这个错误。当我运行 N

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//here we import the three libraries
//we start our main function
int main ()

    setbuf(stdout, NULL);
    //setbuf disables buffering so print statements print properly
    int i,N;

    unsigned int seed;
    double first,second,third,fourth,fifth,sixth,firsttot,secondtot,thirdtot,fourthtot,fifthtot,sixthtot;
    //here we declare the vars to be used
    printf("\nEnter number of iterations and seed");
    printf("\n");
    scanf("%i %u", &N, &seed);
    srand(seed);
    //asks user for input, scans the input, and takes the seed to set a starting point for the rand() function called in the for loop
    //since my R array depends on the user, i declare the array here, after the user inputs the size of the array
    double R[N];
    for (i=0;i<N;i=i+1)
    
        R[i]=(double)rand()/RAND_MAX;
        //printf("%12.8lf \n",R[i]);
    

    //the for loop sets R equal to a random value using our seed with (double)rand()
    printf("\n");
    //here, we have for loops to add up the individual nth moments for each point of the array
    firsttot = 0.0;
    for (i=0;i<N;i=i+1)
    
        firsttot = firsttot + pow(R[i],1);
    
    secondtot = 0.0;
    for (i=0;i<N;i=i+1)
    
        secondtot = secondtot + pow(R[i],2);
    
    thirdtot = 0.0;
    for (i=0;i<N;i=i+1)
    
        thirdtot= thirdtot + pow(R[i],3);
    
    fourthtot = 0.0;
    for (i=0;i<N;i=i+1)
    
        fourthtot = fourthtot + pow(R[i],4);
    

    fifthtot = 0.0;
    for (i=0;i<N;i=i+1)
    
        fifthtot = fifthtot + pow(R[i],5);
    
    sixthtot = 0.0;
    for (i=0;i<N;i=i+1)
    
        sixthtot = sixthtot + pow(R[i],6);
    

    //now, we take the actual nth moment by dividing each total by N;
    first = firsttot/N;
    second = secondtot/N;
    third = thirdtot/N;
    fourth = fourthtot/N;
    fifth = fifthtot/N;
    sixth = sixthtot/N;
    printf("\nThe first moment is:   %lf",first);
    printf("\nThe second moment is:   %lf",second);
    printf("\nThe third moment is:   %lf",third);
    printf("\nThe fourth moment is:   %lf",fourth);
    printf("\nThe fifth moment is:   %lf",fifth);
    printf("\nThe sixth moment is:   %lf",sixth);
    return 0;

【问题讨论】:

整数溢出导致负值 Rustin 不是说 1000000(100 万)吗?您发布的内容是 10000000(1000 万)。无论哪种方式,一个 32 位数字不会溢出 1000 万(32 位整数可以存储 40 亿+) 虽然我怀疑这可能与您建议的溢出有关。 其实我只是注意到R数组是一个基于N值的变长数组。我可以看到,为了足够高的 N 值而炸毁堆栈 有没有办法让它与一个数组一起工作以获取较大的 N 值?我需要能够用 N = 1,000,000 进行计算,它只适用于 N 的值在 10,000 左右 【参考方案1】:

在您的代码中,您在大小为 N:int R[N] 的堆栈上构造一个数组

我怀疑这会导致足够大的 N 值的堆栈溢出。 如果您将行 int R[N] 替换为 int* R = malloc(sizeof(*R) * N);,您能否查看该行为是否重现

使用 malloc 将堆分配您的 R 数组,而不是使用堆栈分配,这将避免可能的堆栈溢出

【讨论】:

成功了!感谢您的帮助,我不知道 malloc,但这绝对解决了问题。

以上是关于获取退出值:-1,073,741,571 用简单的代码计算第 n 时刻的主要内容,如果未能解决你的问题,请参考以下文章

文件大小换算

SQLserver数据类型

SQL Server数据类型

CF741D(dsu on tree)

Codeforces 571B Minimization

当我选择大于720的矩阵大小时,程序会以错误代码退出。原因是什么? [重复]