当我将 const int 声明/初始化为 5 时,它被初始化为一个大数字

When I declare/initialize a const int as 5 it is initialized as a large number

本文关键字:初始化 数字 一个 const int 声明      更新时间:2023-10-16

所以我有以下代码,当运行时会抛出堆栈溢出异常和访问冲突写入位置......错误。特别是在 main(( 的第一行。当我进入调试器时,它将 aSize 的值声明为 1741172928。它在说"异常未处理"和"抛出异常"之间来回切换。错误列表中显示的确切错误比我认为的要长,所以我会推迟。提前感谢您的任何帮助!

编辑:addWithCuda(...(方法在整个事情之前被确认有效,所以我认为它与此无关。在调试器中,无论如何,它甚至无法走那么远

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <math.h>
#include <stdio.h>
#include <iostream>
#define HEIGHT 800
#define WIDTH 800
#define blockLength 8
using namespace std;
int main(){
const int aSize = 5;
const int a[aSize] = { 1, 2, 3, 4, 5 };
const int b[aSize] = { 10, 20, 30, 40, 50 };
const int c[aSize] = { 0 };
// Add vectors in parallel.
cudaError_t cudaStatus = addWithCuda(c, a, b, aSize);
if (cudaStatus != cudaSuccess) {
    fprintf(stderr, "addWithCuda failed!");
    return 1;
}
printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}n",
    c[0], c[1], c[2], c[3], c[4]);


//Zoom=2^zoomfactor
const double zoomFactor = 0;
const double zoom = pow(2,zoomFactor);
//Displacement of the center of the image with respect to the origin
const double delX = 0;
const double delY = 0;
const double minX = -2 / zoom + delX;
const double maxX = 2 / zoom + delX;
const double minY = -2 / zoom + delY;
const double maxY = 2 / zoom + delY;
//Maximum number of iterations 
const int maxIterations = 50*(1+round(zoomFactor));
//Magnitude threshold for convergence
const int threshold = 4;
//Array to keep track of returned iteration counts
int iterationCount[HEIGHT*WIDTH];
//

// cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
    fprintf(stderr, "cudaDeviceReset failed!");
    return 1;
}

return 0;}

(编者注:将OP的答案移到了答案框中(


问题是:

int iterationCount[HEIGHT*WIDTH];

对于堆栈来说太大了。

int * iterationCount=new[HEIGHT*WIDTH];

修复此问题。