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.
Related
I am seeing a C4389: "'==': signed/unsigned mismatch" compiler warning when I execute the following code in Visual Studio using the x86 compiler using a warning level of 4.
#include <algorithm>
#include <vector>
void functionA()
{
std::vector<int> x(10);
for(size_t i = 0; i < x.size(); i++)
{
if (std::find(x.begin(), x.end(), i) != x.end())
continue;
}
}
Can someone explain to me why this happens and how I can resolve this?
Here is a link https://godbolt.org/z/81v3d5asP to the online compiler where you can observe this problem.
You have declared x as a vector of int – but the value you are looking for in the call to std::find (the i variable) is a size_t. That is an unsigned type, hence the warning.
One way to fix this is to cast i to an int in the call:
if (std::find(x.begin(), x.end(), static_cast<int>(i)) != x.end())
Another option (depending on your use case) would be to declare x as a vector of an unsigned integer type:
std::vector<unsigned int> x(10);
#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.
Well i just wanted to do a "foreach" like statement in C++, but in reverse order. Reading a little, the doc says that adding this to the "especial" form of the for statment it can be done.
Like this:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vecInt;
for (int i = 0; i < 10; i++)
vecInt.push_back(i);
for (auto const& x : vecInt | std::views::reverse) //Here is the problem with g++ -std=c++2a
{
}
return 0;
}
The thing is that im using ubuntu linux with the latest g++ compiler.
According to what the compiler throws me. I put an "std=c++20", but it then told me: "Did you mean std=c++2a" so, i don't know what the "a" means, but i tought it would be the C++20 standard. And finally discovered that "std::views::reverse" isn't seen by the compiler.
To go to the point: i want to do a reverse "foreach" C++ statement and i can't.
Any help?
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
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.