Why a variable length array compiles in this c++ program? - c++

It is said that arrays are allocated at compile time, then the size must be const and available at compile time.
But the following example also works, Why?
#include <iostream>
#include <vector>
using namespace::std;
int main()
{
vector<int> ivec;
int k;
while(cin>>k)
ivec.push_back(k);
int iarr[ivec.size()];
for (size_t k=0;k<ivec.size();k++)
{
iarr[k]=ivec[k];
cout<<iarr[k]<<endl;
}
return 0;
}

Compile your code with -pedantic.
Most compilers support variable length arrays through compiler extensions.
The code works due to the compiler extensions, However as you noted the code is non standard conforming and hence non portable.

Related

C++ allows me to allocate an array on run time as opposed to giving an error

I read that array size needs to be known at compile-time. However, when I do this, it compiles and runs just fine without giving any errors...how come?
#include <iostream>
int main() {
int size;
std::cout << "Enter size: ";
std::cin >> size;
int a[size];
return 0;
}
You aren't compiling it as strictly conforming C++, but using an extension borrowed from C99.
Use -Wall -Wextra -pedantic -std=c++14 to make the compiler complain.
And remember that a conforming compiler only needs to output a single diagnostic on encountering a construct the standard deems ill-formed.
Variable length arrays are a reality in C++ and apparently also in C.
https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html

C++ std::array don't work on my local compiler

I follow this example array::size, but it's don't work on my Dev C++ IDE, and I don't know why.
I tried to run on Code Blocks but it's still the same.
I think code is correct but how to fix this problem
here is my code
#include <iostream>
#include <array>
using namespace std;
void useArray()
{
array<int,4> myInts;
myInts[0]=1;
cout<<"Size of Array: "<<myInts.size();
}
int main()
{
useArray();
system("pause");
}
And this is compiler error:
std::array is C++11 only. Dev-C++ does not support C++11.
If you're just looking for options to Visual Studio this article is the right starting place: http://www.cplusplus.com/articles/36vU7k9E/
In the case that you're using a compiler that does not support C++11 (using the flag: -std=c++11) you always have the chance to use a Boost alternative.
Boost comes with the Boost Array Library which is an STL conform Array. The generated code will be similar to a C Array while having the opportinity to use additionnal methods on it e.g determinate the array size. All this basically works as the C++11 version of an Array.
It lives in the 'boost' namespace instead of 'std' therefore you need to include the following header to use it:
#include <boost/array.hpp>
You should then set a typedef declaration for the Boost Array:
typedef boost::array<std::string, 3> array;
// The 3 stands for the number of elements the array can hold
Implementing one of these Arrays is then fairly simple:
array a;

Compilation error for union

can somebody please explain why the following program causing the compilation problem. I have compiled the source code over VS2013.
#include <iostream>
using namespace std;
// Do not work
union myuni
{
string str;
};
void main()
{
}
Does union require the fixed length size while declaring it? The same scenario works fine with structure.
You cannot have a string in a union as the former contains a constructor.
(Although allowed in C++11 this is not supported in VS2013).

unordered_map error in GCC

When was the unordered_map concept built into g++?
Because the following code throws an error.
#include<iostream>
#include<unordered_map>
#include<stdio.h>
using namespace std;
std::unordered_map<std::int,int> mirror;
mirror['A'] = 'A';
mirror['B'] = '#';
mirror['E'] = 3;
int main(void)
{
std::cout<<mirror['A'];
std::cout<<mirror['B'];
std::cout<<mirror['C'];
return 0;
}
I am compiling the code as follows:
g++ -c hashexample.cpp
g++ -o result hashExample.o
./result
The error I got is this:
inavalid types int[char[ for aaray subscript
What is the fix for this?
The problem is your assignment. You cannot assign values to your map in this place. C++ is not a script language.
This program works fine on my machine with gcc4.6:
#include<iostream>
#include<unordered_map>
std::unordered_map<int,int> mirror;
int main() {
mirror['A'] = 'A';
mirror['B'] = '#';
mirror['E'] = 3;
std::cout<<mirror['A'];
std::cout<<mirror['B'];
std::cout<<mirror['C'];
}
First, as mkaes points out, you cannot put assignments outside functions, so you have to put it in any, for example main.
As for unordered_map, for recent versions of gcc, if you don't want to go into C++11, you can use the TR1 version of unordered_map:
#include <tr1/unordered_map>
and the type std::tr1::unordered_map. You know, C++11 supersedes all this, but you will (at least in GCC) get this working.

ICC segfaulting with variable length arrays

So, when compiled with the basic icc bob.cpp -o bob and run, the following code segfaults:
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[foo()];
}
The following two similar programs, however, seem to run fine.
#include <string>
int foo () {
return 6;
}
int main() {
int f = foo();
std::string t[f];
}
and
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[6];
}
I'm a bit confused about what's going on. Apparently, variable length arrays are non-standard, and this was a surprise to me since I've always used g++ which supports it. However, if it's not supported by ICC, why would it compile? Also, why would example 2 "work"?
What is correct code here, and, if the first snippet is incorrect, why does it compile, and then why does it segfault?
I'm using icc (ICC) 12.0.2 20110112 on 2011 x86_64 Intel(R) Core(TM) i5.
Thanks
Well, while it is true that C++ has no variable-length arrays (C99 does though), apparently ICC does support them as an extension, since your code actually compiles (and since your second snippet actually runs without crashing).
If the first version crashes, then it must be a bug in ICC's implementation of that non-standard extension.