Error related to defining and initialization part of an array [duplicate] - c++

#include <iostream>
using namespace std;
int main(){
int n=10;
int a[n];
for (int i=0; i<n; i++) {
a[i]=i+1;
cout<<a[i]<<endl;
}
return 0;
}
worked fine in Xcode4 under Mac
as said in books, it should be wrong, why?
so confused~

This a a C99 feature called VLA which some compilers also allow in C++. It's allocation on stack, just as it would be with int a[10].

That is C99 feature that allows VLA (variable length array).
Compile it with g++ -pedantic, I'm sure that wouldn't compile.

Related

Why is this code running without vector header file?

I couldn't find out why is the code below working fine on my local system without including the vector header file but not on online judges or online compilers.
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
vector<int> v(10);
for(int i = 0; i<10; i++) v[i] = i;
sort(v.begin(),v.end());
for(int i = 0; i<10; i++) cout<<v[i]<<" ";
return 0;
}
I am compiling the code by enabling the warning flags as g++ -Wall -Wextra ./ex.cpp but g++ doesn't give me any warnings at all. Removing the #include<algorithm> does give me the error I wanted, identifier "vector" is undefined, but I don't know what's the relationship between them.
Your algorithm header itself includes the vector header (either directly or indirectly). Because of this, the code after the preprocessor looks the same as if you had included the vector header yourself.
You should not rely on this behavior though, as it depends on the standard library implementation you are using and can change at any time.

Error with Lambda Expression C++ in VS Code

I'm trying to sort an array m of class measure using a lambda expression as follows:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
struct measure{
int day;
int cow;
int change;
};
int main()
{
int N;
cin >> N;
measure m[N];
for (int i = 0; i < N; i++){
measure m_i;
cin >> m_i.day >> m_i.cow >> m_i.change;
m[i] = m_i;
}
sort(m, m + N, [](measure a, measure b) {return a.day < b.day;});
}
However, an error occurs when trying to build the task in VS Code (using C++17):
error: expected expression
sort(m, m + N, [](measure a, measure b) {return a.day < b.day;});
^
1 error generated.
Build finished with error(s).
I've tested this code on other compilers with no difficulties. Why is this error happening on VS Code?
Okay, so it does not look like you are compiling with c++17. I can reproduce this error if I roll back the gcc version to 4.x and leave the standard up to the compiler. Here is how it would look on an older compiler. Here is how it would run properly on a newer compiler. Most likely that you are using something like c++98 or c++03.
Note - I have taken the liberty of modifying your code to make it more C++. Also, please stop with using namespace std.
A simple fix is to just make sure that you are using the right version of the compiler basis the features you need. Lambdas in C++ were introduced in C++11, so clearly you are using a compiler that is at a much lower version.
VS Code does not have a compiler built-in. It uses the system compiler you have installed. You can configure things to use the system compiler and pass the flag --std=c++17 to the compiler command line.
Both gcc and clang++ support this command line flag.

What is "int (*arr)[cols]" where "cols" is a variable, in C++?

I am reading this to consider about how to dynamically allocate memory for a two-dimensional array.
I notice that a variable value cols can be used as size to define int (*arr)[cols], as C language has variable-length arrays(VLA) feature, then I try modifying the code into C++ like:
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
void* allocate(size_t rows, size_t cols)
{
int (*arr)[cols] = (int (*)[cols])malloc(rows *sizeof(*arr));
memset(arr, 0, rows *sizeof(*arr));
return arr;
}
int main() {
size_t rows, cols;
scanf("%zu %zu", &rows, &cols);
int (*arr)[cols] = (int (*)[cols])allocate(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%3d", arr[i][j]);
}
printf("\n");
}
}
compile with gcc 11.2 -std=c++11
To my surprise, this works well and compiler does not report any warning. AFAIK C++ has no VLA feature, I used to think this code should be forbidden. So why could this work?
-std=c++11 doesn't mean "compile strictly according to C++11" but "enable C++11 features." Just as -std=gnu++11 (the default setting) means enable gnu++11 features, which is a superset of C++11.
To get strictly compliant behavior, you must use -std=c++11 -pedantic-errors. And then you get this:
error: ISO C++ forbids variable length array 'arr' [-Wvla]
See What compiler options are recommended for beginners learning C? for details. It was written for C but applies identically to g++ as well.

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

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

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.