1

I am trying to get code that was working on Linux to also work on my Windows 7.

When I retried the same code, it crashed with stack overflow. I then removed everything I could to find out the line which is causing it to crash, and it left me with this:

#include <stdio.h>
#include <stdlib.h>

#include <cuda_runtime.h>

/* 256k == 2^18 */
#define ARRAY_SIZE 262144
#define ARRAY_SIZE_IN_BYTES (sizeof(float) * (ARRAY_SIZE))


int main(void)
{

    float a[ARRAY_SIZE] = { };

    float result = 0;

    printf("sum was: %f (should be around 1 300 000 with even random distribution)\n", result);

    return 0;
}

If I change ARRAY_SIZE to 256, the code runs fine. However with the current value, the float a[ARRAY_SIZE] line crashes runtime with stack overflow. It doesn't matter if I use float a[ARRAY_SIZE]; or float a[ARRAY_SIZE] = { };, they both crash the same way.

Any ideas what could be wrong?

Using Visual Studio 2010 for compiling.


Ok, the stack sizes seem to be explained here, saying 1M is the default on Windows.

Apparently it can be increased in VS 2010 by going Properties -> Linker -> System -> Stack Reserve Size and giving it some more. I tested and the code works by pumping up the stack to 8M.

In the long run I should probably go the malloc way.

eis
  • 51,991
  • 13
  • 150
  • 199

2 Answers2

1

Your array is too large to fit into the stack, try using the heap:

float *a;
a = malloc(sizeof(float) * ARRAY_SIZE);

Segmentation fault when allocating large arrays on the stack

Community
  • 1
  • 1
tesseract
  • 891
  • 1
  • 7
  • 16
0

Well, let me guess. I've heard default stack size on Windows is 1 MB. Your ARRAY_SIZE_IN_BYTES is exactly 1 MB btw (assuming float is 4 bytes). So probably that's the reason

See this link: C/C++ maximum stack size of program

Community
  • 1
  • 1
  • right. The stack sizes seem to be specified here: http://www.cs.nyu.edu/exact/core/doc/stackOverflow.txt, saying 1M is the default on Windows, and apparently it can be increased in VS 2010 by going Properties -> Linker -> System -> Stack Reserve Size and giving it some more. In the long run I should probably go the malloc way. Thanks. – eis Mar 13 '14 at 19:20