views:

613

answers:

7

I have a very large array which must be 262144 elements in length (and potentially much larger in future). I have tried allocating the array on the stack like so:

#define SIZE 262144
int myArray[SIZE];

However, it appears that when I try and add elements past a certain point, the values are different when I try to access them. I understand that this is because there is only a finite amount of memory on the stack, as opposed to the heap which has more memory.

I have tried the following without much luck (does not compile):

#define SIZE 262144
int *myArray[SIZE] = new int[SIZE];

And then I considered using malloc, but I was wondering if there was a more C++ like way of doing this...

#define SIZE 262144
int *myArray = (int*)malloc(sizeof(int) * SIZE);

Should I just go with malloc?

+1  A: 

As the number is not necessarily known at compile time, the type is a pointer:

int *myArray = new int[262144];
Johannes Schaub - litb
Thanks that compiles. However, even after using heap memory, I still have random numbers at the end of my array after assigning. Any ideas?
nbolton
I don't know. Post the way you assign into the question, so all of us can have a look at it
Johannes Schaub - litb
+1  A: 

new allocates on the heap.

#define SIZE 262144
int * myArray = new int[SIZE];
Mitch Wheat
A: 

The reason the first try didn't work is that the syntax is incorrect. Try this:

int *myArray = new int[SIZE];
Tommy Hui
+1  A: 

I believe

#define SIZE 262144
int *myArray[SIZE] = new int[262144];

should be

#define SIZE 262144
int *myArray = new int[262144];

or better yet

#define SIZE 262144
int *myArray = new int[SIZE];
John MacIntyre
A: 

Your syntax for using new was wrong, it should be:

int *myArray = new int[262144];

you only need to put the size on the right of the assignment.

However, if you're using C++ you might want to look at using std::vector (which you will have) or something like boost::scoped_array to make the the memory management a bit easier.

Wilka
std::vector should be recommended as well, since it's *actually* part of C++, and not just a "pretty good addition" to the language.
Tom
That's a very good point
Wilka
+14  A: 

You'll want to use new like such:

int *myArray = new int[SIZE];

I'll also mention the other side of this, just in case....

Since your transitioning from the stack to the heap, you'll also need to clean this memory up when you're done with it. On the stack, the memory will automatically cleanup, but on the heap, you'll need to delete it, and since its an array, you should use:

delete [] myArray;

Reed Copsey
John MacIntyre
+13  A: 

The more C++ way of doing it is to use vector. Then you don't have to worry about deleting the memory when you are done; vector will do it for you.

#include <vector>

std::vector<int> v(262144);
Brian Neal
+1, but boost::shared_array *may* be more appropriate for super sized arrays depending on the use case.
Functastic
It sounds as if vector is perfect for his case. If an array would have worked on the stack, a vector will replace the array just fine.
Zan Lynx
Yeah I considered vector... I just figured array would be better memory wise, but thinking about it, that doesn't make sense... vector may even be more memory efficient, right?
nbolton
@Nick: well it won't be more efficient, but it will be far easier and safer to use.
Brian Neal
Why boost::shared_array? The OP isn't asking about reference counting, and std::vector is sufficent for most cases. That, and std::vector optimizes down to a bald array, which you really can't beat without an algorithmic change.
Tom
@Tom: Perhaps the memory block is being passed to an API such as NtQueryInformationFile which copies data into it at variable offsets, for example. The operations provided by vector don't really make sense in that case.
Functastic
But I'm not saying the OP _should_ use shared_array. Just that vector isn't suited to all use cases.
Functastic
Brian Neal