-2

I try to compile some c++-Code from the internet (http://arma.sourceforge.net/shadows/).

When compiling the code I get an error for initializing arrays. Example (from the code-> GaussianMixtureModel.cpp Line:122):

void function()
{
  int k = Vector.size();
  uchar* Ptrs[k];
  // Does somthing with the Ptrs
}

I also tried to edit it to the following:

const int k = Vector.size();

But it didn't work. I would appreciate any help!

I'm using Visual Studio 2012.

Thanks for your answers!

NewTech
  • 241
  • 1
  • 3
  • 14

4 Answers4

3

Arrays of variable length are not standard C++, they are a compiler extension in gcc and clang.

Looks like the code you are trying to compile needs to be compiled with one of the above.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
3

What that piece of code is trying to use is called VLA - variable length array (yes, it's still variable in this context even if you make it const). You can read more about how it (doesn't) work in Visual Studio and why it doesn't here:

Community
  • 1
  • 1
Ionut
  • 6,436
  • 1
  • 17
  • 17
2

As Baum mit Augen pointed out, visual studio does not support a non-standard language extension for variable length arrays.

To make the program standard compliant, you can use a dynamically allocated array instead:

auto Ptrs = std::vector<uchar*>(k);

Some other changes may be necessary depending on how Ptrs is used.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I think this is the idiomatic answer. No use keeping an array when OP is already using `std::vector`s through the code. RAII is the way to go. Might prefer smart pointers to raw, but that's really minutia and dependent on what OP wants. – erip Feb 29 '16 at 14:07
  • @erip an array already conforms to RAII idiom. The advantage of `std::vector` in this case is that it's size doesn't need to be known at compile time. Without context, it's impossible to say whether OP should prefer a smart pointer but since it's not mentioned otherwise, I would assume that the pointers are not owning. – eerorika Feb 29 '16 at 14:41
  • Can't keep a variable length array, so would need to dynamically allocate memory - not RAII, right? As for smart vs. raw, I totally agree and am kind of thinking that my last comment is probably a moot point. – erip Feb 29 '16 at 15:06
  • 1
    @erip fair enough. I had understood that you were comparing the `std::vector` solution to what OP originally had. But yes, RAII is indeed one of the reasons to prefer `std::vector` to manual dynamic allocation. – eerorika Feb 29 '16 at 15:13
1

In standard C++ you can only define arrays with a compile time constant length. That means, you can not use k, since it is determined at runtime. The code you got from the internet probably uses an extension called "variable length array" (VLA) which Visual Studio does not implement.

You could instead define a vector of uchar* if the semantics of the vector cleaning up its memory is the right thing in your case:

void function() {
  auto vecSize = Vector.size();
  auto Ptrs= vector<uchar*>(k, nullptr);
  // Does somthing with the Ptrs
}
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90