2

How can I set the size of my array equal to the size of my vector. Here's my code:

vector<Point> data_obj;
int my_array[data_obj.size()]

but I get compile errors saying:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0

I am not understanding the error. Could you please provide some explanation?

Jeff B
  • 8,572
  • 17
  • 61
  • 140
Arsalan Sherwani
  • 889
  • 2
  • 25
  • 60
  • 1
    You can't. The size of arrays in C++ needs to be known at compile time. Why do you need this anyway? Just keep using vector. If you need the underlying array either do `&data_obj[0]` or call `data_obj.data()`. – Borgleader Dec 03 '14 at 13:35
  • I am just trying to set the array size equal to vector size , so whenever the vector data increases the array size adjust to it. – Arsalan Sherwani Dec 03 '14 at 13:39
  • 2
    @ArsalanSherwani ...arrays don't adjust. That is what `std::vector` is for (and why you generally use it instead). – crashmstr Dec 03 '14 at 13:40
  • 4
    You can't do this in (standard) C++. Why do you want a separate array? Can you just use `data_obj.data()` to access the array managed by the vector? If not, copy the vector and use the array from that copy. – Mike Seymour Dec 03 '14 at 13:40
  • [C++ doesn't support variable length arrays](http://stackoverflow.com/questions/1887097/variable-length-arrays-in-c) like C (since C99), however [gcc supports it in C++ as an extension](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html). Moreover you can't simply declare arbitrary sized arrays on stack because large arrays will cause the stack to overflow – phuclv Dec 04 '14 at 09:51

3 Answers3

5

As the error says, the size of a static array must be constant at compile time. It is possible that the size of the std::vector may change during run time, so the size of the array is not guaranteed to be constant. You'd have to make a dynamic array

int* my_array = new int[data_obj.size()];

And remember to delete it after

delete[] my_array;
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 2
    Although a better way to make a dynamic array would be `std::vector my_array = data_obj;` (assuming you can't just use `data_obj.data()`). – Mike Seymour Dec 03 '14 at 13:38
  • 1
    @MikeSeymour That's how these questions always tend to pan out. "I need an array, the size of which is not know at compile time, and not constant at run time". So just use a vector! Yet, they started with a vector.... so it seems we're going taking steps backwards :P – Cory Kramer Dec 03 '14 at 13:40
  • @Cyber Reading between the lines what they probably *actually* want is `std::vector::data()` – sjdowling Dec 03 '14 at 13:50
  • thanks @Cyber, it worked for me understanding things of vector – Arsalan Sherwani Dec 03 '14 at 13:53
0

error C2057: expected constant expression

data_obj.size() is not constant when you allocate an array, but it should be - this is one of the requirements. If you want to do something like this, you need dynamic allocation.

error C2466: cannot allocate an array of constant size 0

in this case (when you didn't put anything to vector), its size is zero. You cannot create array with size 0.

  • You can create an array with size 0, just not statically: `int *my_array = new int[0];`. – Noel Dec 03 '14 at 13:40
-3

The matter is that arrays allocated through "classic" syntax (e.g. int a[5]) are allocated on stack. Therefore, you need to know at compile time how large the array is going to be, as the compiler needs to change the SP (stack pointer) value. On the other hand, vector's memory area is on the heap, like arrays allocated through malloc/new. Therefore you can wait till run time, to know how much memory you need and allocate it consequently.

Stack/vs heap allocation also has other pros/cons: stack allocation is way faster, since you just need to decrease a register value, but stack is limited in size and if your array is too large you'll end up with a Stack Overflow exception, which may lead to weird stuff happen like creating a wormhole dumping this entire site on your HDD :-) . Heap on the other hand is slower and too many allocation/deallocations may lead to fragment memory space.

Marco Veglio
  • 332
  • 1
  • 8
  • Arrays can be allocated pretty much anywhere. – MSalters Dec 04 '14 at 08:06
  • I specified that arrays allocated with new/malloc go to heap, but arrays allocated through "classic" array sintax (e.g. int a[5]) are on stack. Why downvote? – Marco Veglio Dec 04 '14 at 08:27
  • 1
    Well, there are global arrays, string literals (which are global-like), arrays in structures on the heap, statics, and that's all "classic syntax" but not on the stack. Also, C'99 shows that you can in fact change the SP by a runtime amount, so it's not a technical constraint either. – MSalters Dec 04 '14 at 08:51
  • `int a[5];` could be on "stack", "heap", or static data area, or thread-local storage – M.M Dec 04 '14 at 08:56