28

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
  • 1
    change "myArray[SIZE]" to "myArray" in your second block of code. – eduffy Mar 24 '09 at 01:10
  • 4
    Vijay, this isn't a question of learning the syntax. The first option is correct, except for the size, and the second option is a common enough bug that it deserves mention. Besides, isn't this site also about helping when someone doesn't understand the syntax? – Nathan Fellman Mar 24 '09 at 10:08
  • 3
    To have a syntax error in a question doesn't make it a bad question. – Lena Schimmel Mar 24 '09 at 10:08
  • 3
    Straight from the FAQ: No question is too trivial or too "newbie". Be tolerant of others who may not know everything you know. Bring your sense of humor. – E Dominique Mar 24 '09 at 11:55
  • Vijay, perhaps this site isn't for you. Forums are often the best place to start flame wars, you should check them out. My experience of Stack Overflow has been for users to ask anything they feel like asking, regardless of how newbish. – Nick Bolton Mar 24 '09 at 13:15

7 Answers7

73

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;
Matthew D. Scholefield
  • 2,977
  • 3
  • 31
  • 42
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
26

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
  • 31,821
  • 7
  • 55
  • 59
  • +1, but boost::shared_array *may* be more appropriate for super sized arrays depending on the use case. – Functastic Mar 24 '09 at 02:36
  • 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 Mar 24 '09 at 02:41
  • 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? – Nick Bolton Mar 24 '09 at 02:44
  • 1
    @Nick: well it won't be more efficient, but it will be far easier and safer to use. – Brian Neal Mar 24 '09 at 03:25
  • 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 Mar 24 '09 at 04:25
  • @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 Mar 24 '09 at 15:59
  • But I'm not saying the OP _should_ use shared_array. Just that vector isn't suited to all use cases. – Functastic Mar 24 '09 at 16:00
  • 1
    @Cache: You can easily pass vector data to api's expecting pointers. The elements of vector are guaranteed to be contiguous. &v[0] lets you get a pointer to the internal buffer. – Brian Neal Mar 24 '09 at 16:18
3

new allocates on the heap.

#define SIZE 262144
int * myArray = new int[SIZE];
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
2

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

int *myArray = new int[SIZE];
Tommy Hui
  • 1,306
  • 6
  • 9
2

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
  • 28,701
  • 14
  • 75
  • 97
  • 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 Mar 24 '09 at 04:27
1

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

int *myArray = new int[262144];
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
1

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
  • 12,910
  • 13
  • 67
  • 106