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?

5
  • 1
    change "myArray[SIZE]" to "myArray" in your second block of code.
    – eduffy
    Mar 24, 2009 at 1: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? Mar 24, 2009 at 10:08
  • 3
    To have a syntax error in a question doesn't make it a bad question. Mar 24, 2009 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. Mar 24, 2009 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. Mar 24, 2009 at 13:15

7 Answers 7

74

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;
0
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);
8
  • +1, but boost::shared_array may be more appropriate for super sized arrays depending on the use case.
    – Functastic
    Mar 24, 2009 at 2: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, 2009 at 2: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? Mar 24, 2009 at 2:44
  • 1
    @Nick: well it won't be more efficient, but it will be far easier and safer to use.
    – Brian Neal
    Mar 24, 2009 at 3:25
  • 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, 2009 at 16:18
3

new allocates on the heap.

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

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

int *myArray = new int[SIZE];
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.

1
  • 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, 2009 at 4:27
1

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

int *myArray = new int[262144];
2
  • Thanks that compiles. However, even after using heap memory, I still have random numbers at the end of my array after assigning. Any ideas? Mar 24, 2009 at 1:12
  • I don't know. Post the way you assign into the question, so all of us can have a look at it Mar 24, 2009 at 1:16
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];

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.