当我将 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];
修复此问题。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 是否可以初始化不可复制类型的成员变量(或基类)
- C++使用整数的压缩数组初始化对象
- C++初始化基类
- 多成员Constexpr结构初始化
- 复制列表初始化的隐式转换的等级是多少
- 内联映射初始化的动态atexit析构函数崩溃
- 使用从1到N的数字初始化std数组模板参数
- 使用数字初始化常量引用
- 成员初始化列表中初始化的向量与传递给构造函数的数字不同
- 设置::使用数字中的引号进行向量初始化
- 初始化具有递增数字的编译时常量大小的数组
- 如何初始化字符串以包含C 中所需数量的数字数量
- 如何使用整数值中的数字符号初始化字符数组
- 添加大数字C++-初始化程序失败
- 使用一系列数字有效地初始化 std::set
- 初始化成员函数字段
- Array[n]vs Array[10]-使用变量与数字文本初始化数组
- 大数字类,初始化
- c++中未初始化数组时出现奇怪的数字