Visual Studio 2012 C++ arrays initialization using { } - c++

i've just started programming in visual studio 2012 Express and from the beginning I'm having problems with arrays.
The environment says that this code is invalid:
int a[10] = {5,1,8,9,7, 2,3,11, 20,15};
First of all i had to declare that this array has fixed size using fixed keyword, but after that the program still has been wanting to put ; after a[10]. Filling up this array one number by one would be waste of time. Is it possible to work around it? I can't find any solution in google so I decided to post my problem here.

There's no fixed keyword in C++, perhaps in C#
The code you posted is perfectly valid in VS2012 Ultimate (and probably also Express)
From the above I might conclude you mismatched project and are trying to compile a C++ code in a C# environment.
Another reason that makes me think the above is the following error you get in a C# project if you try to compile the snippet above:
error CS0650: Bad array declarator: To declare a managed array the
rank specifier precedes the variable's identifier. To declare a fixed
size buffer field, use the fixed keyword before the field type.
which refers exactly to the fixed keyword you were trying to use.
Short story: you're trying to compile a C++ code in a C# project. Paste that code in a C++ project, not a C# one. Those are two different languages.

May be its too late to but you can use STL array for fix size arrays as
#include <array>
std::array<int, 5> ary { 1,2,3,4,5 }
This will be a fixed size array
As mentioned by Marco A. there is no "fixed" keyword in C++

Related

C++ dynamic arrays. Why does this one work?

I was looking at this algorithm, the second one: Dynamic Programming Solution
http://www.geeksforgeeks.org/dynamic-programming-set-24-optimal-binary-search-tree/
It creates a dynamic array: int cost[n][n];
How does this work? I can run the code on GeeksForGeeks C++ emulator, but locally in Visual Studio I get the error "Expression must have a constant value".
What am I misunderstanding here? Doesn't C++ need to know the size of the array before compiling?
The code is not standard.
type name[runtime_size]
Is what is called a variable length array. This is not standard in C++ and will only compile in compilers that have an extension for this like g++ or clang. The reason this extension exists is it is valid in C99.
You are completely correct that the size of the array must be known at compile time. If you need an array and the size will not be know until run time I suggest you use a std::vector or a std::unique_ptr<type[]>.

Init Array of vector.size() in c++ [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 6 years ago.
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!
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.
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:
C++ Variable expressions for defining array size?
Enabling VLAs(variable length arrays) in MS Visual C++?
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.
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
}

Declaring and initializing very large arrays on the heap

I have an array of about 66,000 elements, with each element being a POD struct of integral data types. This array is constant and will never change, so my original thought was to just put it in as a constant global variable.
I declared it in a header as extern and initialized it in a cpp file, like (obviously simplified here):
const PODStruct bigArray[] =
{
{1,2,3,4} , {1,2,3,5} , ....
}
with some editing in a text editor so it wasn't just one continuous line.
--EDIT: I was reminded that global variables are of course not stack-allocated, so the paragraph that was here went away! However, what if I still would rather have the data in a vector?
I was thinking since C++11 allows that same syntax for std::vector initialization, I could just use that with a simple edit and have a vector instead. However, in MSVC++ 2013, when I attempt to compile it says I've hit compiler limits. I looked through the C++ standards for compiler limits and MSVC++13's deviations from it, but nothing seemed to be directly the cause. I'm guessing it has to do with how that initializer-list syntax is actually implemented.
I can get the array itself into a vector using the constructor in the answer here: How to initialize std::vector from C-style array?
However, then I'd still have the array in memory twice, right? It's not on the stack like I originally feared and not that big, so it's not a huge deal, but seems like a sloppy solution.
I'm thinking I could create a class with a default constructor, and declare and initialize the typed-out table in there. Then I can declare the vector in the constructor and construct it with the array. The class's only member could just be the vector.
I could just declare that class then create a global instance of it, right? That'd be similar to the behavior I had with the global array. If I wanted to get away from that, is the best approach to declare the class first thing under main, and then pass it around to the functions and methods that need the table?
Should I want to get away from that? This data is, despite a lot of it, along the lines of PI = 3.4.
Your idea about storing your "huge constant array" in a compile-size generated constant is ok, that what I'd do.
If you try to move all this to a vector or other variant of heap-allocated array, then you'd simply duplicate the data, since the initialization data resides in your executable image anyway.
To workaround the (idiotic) MSVC 2013 compiler limit that's what I'd try.
Switch to MSVC 2010 compiler. See the build options for your .cpp file, in MSVC 2013 you may set the "platform toolset" of MSVC 2010.
Try to redefine your data type. For instance, instead of having an array of structs, try an array of (constant) pointers to structs. All this should be compile-time generated as well.
With some efforts most probably you may work this around. Good luck.
It seem to me that you have hit some unknown file size barrier on MSVC and/or your computer hardware. Try load the data from an external file maybe (using mmap(2) preferably?)
Not really relevant, since this is a huge amount of data, yu can try look into things like OpenCL or CUDA to let the GPU help you crunch the numbers if possible. It would make things a lot faster.

error C2057: expected constant expression

Doing some AudioDSP in VC++ 2012 and am having problems with allocating memory for the buffer
int size = input.getSize();
float buf[size];
At compile I get the error "error C2057: expected constant expression" Looks like the code works in C99 just not C++. Any idea how to get around this?
That is because C99 supports variable length arrays, whilst C++ does not (at least, not C++98, nor C++11). Here are some workarounds:
Refactor your code to use a std::vector or any suitable container. If possible, this would be the preferred method.
Find a compiler that supports VLAs as an extension (for example, GCC with the option -std=c++gnu98 or -std=gnu++11).
Anyway, if you don't have that much code to refactor, please try to change your code to use a standard container instead of a VLA.

Creating array with constant

I was working on a program in Netbeans on Linux using a gcc compiler when, upon switching to Visual C++ on Windows 7, the code failed to compile as Visual C++ says it expected constant expression on several lines. On Netbeans, I simply did something similar to char name[fullName.size()];, while on Visual C++, I tried, among other things,
const int position = fullName.size();
char Name[position];
How can I create a constant to use for the array?
Note: I know about this question, but is there any way I can get this working without using vectors, since that would require a re-write of many parts of the program?
This is not possible in VC++. I know, pretty sad :(
The solutions include:
Create it on the heap
Make it constant
The new C++ standard (C++0x) proposes a 'constant expression' feature to deal with this. For more info, check this out.
In VC++ you can't do runtime declarations of stack array sizes, but you can do stack allocation via _alloca
so this:
const int position = fullName.size();
char Name[position];
becomes this:
const int position = fullName.size();
char * Name = (char*)_alloca(position * sizeof(char));
It's not quite the same thing, but it's as close as you are going to get in VC++.
C++ requires that the size of the array be known at compile time. If you don't mind using a non-standard extension, gcc does allow code like you're doing (note that while it's not standard C++, it is standard in C, as of C99).
I'd also guess that you could use a vector (in this particular place) with less trouble than you believe though -- quite a bit of code that's written for an array can work with a vector with only a re-compile, and little or no rewriting at all.
Your char name[fullName.size()]; is an example of a variable-length array which - as far as I know - are not standardized in C++ so you're at the mercy of the compiler.
[Slightly off-topic they're part of the C99 standard]