0

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(). When I go into the debugger, it states the value of aSize as 1741172928. It bounces between saying "exception unhandled" and "exception thrown". 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. 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;}
M.M
  • 138,810
  • 21
  • 208
  • 365
  • 4
    `int iterationCount[HEIGHT*WIDTH];` - You probably blew out the stack with this declaration. This is over 2 megabytes in size, and stack space is usually not that large. – PaulMcKenzie Mar 03 '18 at 05:22
  • [Possible duplicate](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) – PaulMcKenzie Mar 03 '18 at 05:28
  • I see no C++ there. Anyway when a debugger breaks at the first line in main, it shows next instruction to be executed. Thus assignment is not executed yet and `aSize` is not initialized. Go over next step to see initialized constant. – 273K Mar 03 '18 at 05:28
  • Are you able to track till where the program executed?, By tracking some of the print statements before the program crashes – Amith Mar 03 '18 at 05:45
  • @PaulMcKenzie I think you were right! I changed it and it works now. Thank you for that. I'm still quite new to memory management. Thank you! – Meekail Zain Mar 03 '18 at 22:42
  • Please don't post Answers in the Question; they go in the Answer box. I have moved the answer now since the question was old. – M.M Dec 03 '18 at 10:51

1 Answers1

-1

(Editor's Note: moved OP's answer to the Answer box)


The issue was that:

int iterationCount[HEIGHT*WIDTH];

was too large for the stack.

int * iterationCount=new[HEIGHT*WIDTH];

fixes this.

M.M
  • 138,810
  • 21
  • 208
  • 365