簡體   English   中英

當我將const int聲明/初始化為5時,它被初始化為一個大數字

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

因此,我有以下代碼,該代碼在運行時會引發堆棧溢出異常和訪問沖突寫入位置...錯誤。 特別是在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];

解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM