What prevents C++ from being a strict superset of C? [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
“C subset of C++” -> Where not ? examples ?
I'm aware that C++ is not a strict superset of C. What language features prevent C++ from being a superset of C?

The elephant in the room: the following is valid C but not valid C++.
int typename = 1;
Substitute your favorite C++ reserved word.

C++ also does not support variable-length arrays, where:
int array[n];
is valid in C, but not C++. A C++ version of the above would be:
int *array = new int[n];
...
delete [] array;

There is a special wiki entry that summarizes a lot of issues.

Simple example, consider this declaration:
int f();
This is valid C, but invalid C++: f(3, 2, -5, "wtf");
Explanation: in C, int f() is treated like int f(...) (at least at the first call site). Declare as int f(void) if you don't want f to take parameters.

One from top of my head - C++ does not support default int.

Related

Visual Studio Community 2019 requires a constant value inside square brackets [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 2 years ago.
I've been trying to find answers for this lately, but I just can't seem to understand why the compilers for C++ that Microsoft has been using can't compile such a code :
#include<iostream>
int main()
{
int n;
std::cin >> n;
int x[n];
}
It gives those errors :
However, this code compiles on a lot of different compilers.
Could someone point me to somewhere, I couldn't find any answers for this.
See How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)? for discussion on the matter. Briefly, C++ requires that raw arrays have a constexpr for their size, so they it knows how much memory to allocate for it at the moment it's declared instead of at runtime after receiving user input. Some compilers I suppose are more permissive on this.
Variable length arrays are not standard C++. Some compilers provide them as a language extension but they are not portable.
This code shouldn't compile.
In C++, arrays are statically allocated and their required memory must be specified at compile time.
Hence, a user input is not a suitable value for array declaration. If you want a dynamic array, you can try using malloc like this:
#include<iostream>
int main()
{
int n;
std::cin >> n;
int* x = (int*)malloc(n*sizeof(int));
}
This way, the array is actually stored in heap, and can have any arbitrary size, changing over lifetime of the program.
Another alternative is std::vector of course.

Does C++11 support variable sized arrays? [duplicate]

This question already has answers here:
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 2 years ago.
I'm very new to C++, but I've been digging around and the general answer is no, c++11 does not support variable sized arrays, since array sizes need to be constant expressions.
However, I've tried this code on XCode 11 (C++11):
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
unsigned long arrayS;
cin >> arrayS;
bool a[arrayS];
return 0;
}
and it totally works. When I set a breakpoint at
bool a[arrayS];
I can see that the array a has arrayS elements. I have verified this with
*(&a + 1) - a
and it shows that the number of elements in a is arrayS.
Does c++11 support variable sized arrays? Or, is it only working for me because of the compiler I'm using?
I'm asking this question because I'm unsure of what compiler my friend is using and want to send the code to him to run.
Any help is much appreciated.
That is non standard C++. Some compilers accept it, but if you want to be portable you should stick with standard C++.
In c++, array size should be supplied at compile time. For variable sized arrays use std::vector

c++ weird array by user input [duplicate]

This question already has answers here:
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 4 years ago.
As I know in c++ if you want to crete an array, you must give constant value for its size. But here:
int main(){
int a;
cin >> a;
int b[a] = {};
for (int i = 0; i<a ; i++){
b[i] = a;
cout << b[i];
}
return 0;
}
If i input 5
output:
55555
It works fine in a way i can't understand in dev c++. If i run this in visual studio 2017, it gives error. Can anyone explanin why?
Are you using GCC by any chance? This is a GCC extension and it's enable by default. In fact it's quite a dangerous one because it's fairly easy to cause a stack overflow on your program. It's roughly the same as using alloca().
In order to disable it, you should use a compiler flag called -Wpedantic. This will make your compiler issue a warning. (see this demonstration)
ISO C++ forbids variable length array ‘b’ [-Werror=vla]
As I know in c++ if you want to crete an array, you must give constant value for its size.
Correct. If you use a non-constant value, then the program is ill-formed. Yes, the program that you show is ill-formed.
It works fine in a way i can't understand ... Can anyone explanin why?
C++ compiler may allow compilation of an ill-formed program. This enables the compilers to extend the language. It appears that you were using a non-standard extension to C++.
This is what the GCC compiler says about your program:
warning: ISO C++ forbids variable length array 'b' [-Wvla]
int b[a] = {};
^

Using variable to initialize an array in C++ [duplicate]

This question already has answers here:
In C++ books, array bound must be constant expression, but why the following code works?
(2 answers)
Closed 5 years ago.
As C++ primer said, we can't use variable as the dimension of builtin array, so the following code did not work
int length = 3;
int array[length] = {0, 1, 2};
the error is
error: variable-sized object may not be initialized
But why following code works?
int length = 3;
int array[length];
This is an extension by your compiler called a Variable Length Array (VLA) and is not in the C++ standard which means that this code can break at any moment you switch compilers or the compiler vendor decides to no longer support this feature. If you want a variable length array that does not depend on this extension but instead on the standard you should use a std::vector.

what does "a<?=b" in C++ mean? [duplicate]

This question already has answers here:
C extension: <? and >? operators
(2 answers)
Closed 9 years ago.
I saw this code
a<?=b; // (a and b are int)
from the solution of Google Code Jam.
but my VS shows an error on '?'
I only know the following:
a>b?a=0:b=0;
Thanks.
Old operator; it's a (since removed) gcc extension for 'minimum'. That is:
a <?= b;
is the same as:
a = a < b ? a : b;
A nonstandard GCC extension to C++ allows <? as an operator which is equivalent to min. I haven't seen <?= before, but presumably it's an in-place version; that is, a <?= b is equivalent to a = min(a,b).
Note that the GCC developers woke up the next morning and realized what a bad idea that was. The operator is now deprecated.
If a happens to be larger than b, it would set a to b.
Essentially the same as:
a = a < b ? a : b;
Example:
int a = 5;
int b = 2;
a<?=b; //a is now 2!
I wouldn't advocate actually using such a solution though, it is horrible.
As others have said, it's part of a nonstandard GCC extension, but please don't use it.