1

Possible Duplicate:
Stack overflow visual C++, potentially array size?

This code is simply meant to read values from a binary file into the array DataBuffer. When the size of DataBuffer is greater than or equal to 515000, it simply crashes. I am developing this in Visual C++ 2010 on Windows 7. The function cbFileRead() is something whose source code I can not access. cbFileRead() expects DataBuffer to be of the type USHORT*.

#include <stdio.h>  // printf()
#include "cbw.h"    // cbFileRead()

int main(int argc, char* argv[]) {

    // Declarations
    char* FileName = argv[1];
    long FirstPoint = 0;
    long NumPoints;

    // Set data collection sizes
    const long chunkSize = 515000;
    NumPoints = chunkSize; // Number of points to be read into mem
    WORD DataBuffer[chunkSize-1];

    // Get data
    cbFileRead(FileName, FirstPoint, &NumPoints, DataBuffer);

    printf("Completed on data point %d whose value is %d\n", NumPoints, DataBuffer[chunkSize-1]);

    return 0;
}

What reasons are there for this crashing? I would expect the array size to be able to go much higher.

11
  • You typically have a pretty small stack by default. I'd use std::vector instead Commented Jul 11, 2012 at 14:10
  • 2
    That looks like C, not C++ ? Why is it tagged as C++ ? Commented Jul 11, 2012 at 14:11
  • 3
    Stack in Windows is around 1MB (or 2), IIRC. Commented Jul 11, 2012 at 14:11
  • 1
    @PaulR: Because it's being compiled by a C++ compiler. Commented Jul 11, 2012 at 14:13
  • 2
    @thoughtadvances you almost certainly don't have a multi-gigabyte stack. It's normally only a megabyte or so. Commented Jul 11, 2012 at 14:14

4 Answers 4

4

The printf() is going beyond the end of the array DataBuffer, as it has chunksize - 1 elements so the last element is chunksize - 1 - 1. The function cbFileRead() is (possibly) misinformed of the number of elements in DataBuffer also.

EDIT:

As others have already stated, the default stack size is 1MB. The size of the DataBuffer array is 2 * 515000 which equals 1030000, which leaves 18576 free bytes on the stack. cbFileRead() could easily be declaring a large buffer on the stack for reading from file. As suggested by everyone else, allocate the DataBuffer on the heap using new[] (and delete[] to free) or use vector<WORD>.

Sign up to request clarification or add additional context in comments.

3 Comments

+1, that's a problem too (didn't scroll his code enough!).
Thanks. FirstPoint = 0, so I think cbFileRead() is getting the correct numbers. I want chunkSize elements, so they are stored in an array of chunkSize() - 1. Because the array is also null indexed, the last element of the array is then chunkSize()-2. Is that right?
@thoughtadvances, but the array was declared as having chunkSize - 1 elements. If it has chunkSize elements then valid indexes are from 0 to chunkSize - 1.
2

The default stack reservation size used by the linker is 1 MB. To specify a different default stack reservation size for all threads and fibers, use the STACKSIZE statement in the module definition (.def) file.

Microsoft Dev Center - Thread Stack Size

Or you can allocate the memory dynamically with the new keyword.

1 Comment

Thanks. I just discovered that new is capable of doing this.
1

Your stack size may not be large enough to handle local data of that size (assuming this is what you mean by "crash"):

// use dynamic allocation instead of stack local
WORD *DataBuffer = new WORD[chunkSize];

cbFileRead(FileName, FirstPoint, &NumPoints, DataBuffer);

// ...use DataBuffer...

// deallocate DataBuffer when done
delete[] DataBuffer;

1 Comment

+1 returned as this a probable cause.
1

On most platforms, including Windows, local variables are stored on a stack, which has a limited size - in this case, it looks like it's around 1MB. There's probably a way to increase that size if you really need to, but it would be better to allocate large arrays dynamically:

#include <vector>

std::vector<WORD> DataBuffer(chunkSize); // guessing that "chunkSize-1" was an error

cbFileRead(FileName, FirstPoint, &NumPoints, &DataBuffer[0]);

printf("Completed on data point %d whose value is %d\n", 
       NumPoints, DataBuffer[chunkSize-1]);

Note that, if the array size is actually supposed to be chunkSize-1, then the last element would be DataBuffer[chunkSize-2], since arrays are indexed from zero.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.