简体   繁体   English

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

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

So I have the following code which when run throws both a stack overflow exception and an access violation writing location ... error. 因此,我有以下代码,该代码在运行时会引发堆栈溢出异常和访问冲突写入位置...错误。 Specifically at the first line in main(). 特别是在main()的第一行。 When I go into the debugger, it states the value of aSize as 1741172928. It bounces between saying "exception unhandled" and "exception thrown". 当我进入调试器时,它表示aSize的值为1741172928。它在说“未处理异常”和“引发异常”之间反弹。 The exact error presented in the error list is longer than I think would be appreciated so I'll hold off on that. 错误列表中显示的确切错误的时间比我认为的要长,因此我将坚持下去。 Thank you in advance for any help! 预先感谢您的任何帮助!

EDIT: The addWithCuda(...) method was confirmed working before this whole thing so I don't think it has anything to do with that. 编辑:addWithCuda(...)方法已被确认可以在整个工作之前进行,因此我认为它与此无关。 In the debugger, it can't even get that far in the first place anyways 在调试器中,无论如何它甚至都无法做到这一点

#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;}

(Editor's Note: moved OP's answer to the Answer box) (编者注:将OP的答案移至“答案”框中)


The issue was that: 问题是:

int iterationCount[HEIGHT*WIDTH];

was too large for the stack. 对于堆栈来说太大了。

int * iterationCount=new[HEIGHT*WIDTH];

fixes this. 解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM