3

I really appreciate this community and all the help it has provided towards my programming problems that I've had in the past.

Now unfortunately, I cannot seem to find an answer to this problem which, at first glance, seems like a no brainer. Please note that I am currently using C++ 6.0.

Here is the code that I am trying to convert from C#:

byte[] Data = new byte[0x200000];
uint Length = (uint)Data.Length;

In C++, I declared the new byte array Data as follows:

BYTE Data[0x200000];
DWORD Length = sizeof(Data) / sizeof(DWORD);

When I run my program, I receive stack overflow errors (go figure). I believe this is because the array is so large (2 MB if I'm not mistaken).

Is there any way to implement this size array in C++ 6.0?

1
  • 1
    Is declaring the array on heap an option? I mean is it possible for you to new this array instead of declaring it on stack? Commented Sep 16, 2011 at 17:43

2 Answers 2

3

Defining array this way makes in on stack which ends in stack overflow. You can create very big arrays on heap by using pointers. For example:

BYTE *Data = new BYTE[0x200000];
Sign up to request clarification or add additional context in comments.

Comments

2

Currently, you are allocating a lot of memory on the thread's stack, which will cause stack overflow, as stack space is usually limited to a few megabytes. You can create the array on the heap with new (by the way, you are calculating the array length incorrectly):

DWORD length = 0x200000;
BYTE* Data = new BYTE[length];

You might as well use vector<BYTE> instead of a raw array:

vector<BYTE> Data;
int length = Data.size();

3 Comments

When I try to compile with the vector<BYTE> declared, the compiler states "vector is an undeclared identifier". Is there a header I need to include?
@Brandon: #include <vector> and using std::vector;
#include <vector> - really if you couldn't figure that out with google you're making no attempt to help yourself. Also not knowing about stack vs heap and the STL indicates to me that you could help yourself immensely by buying a book that covers C++ and the basics of the STL. What no one mentioned yet is that also need to delete that array with the proper delete[] syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.