I understand that VLAs are not part of C++11 and I have seen this slip by GCC. It is part of the reason I switched to Clang. But now I am seeing it Clang too. I am using clang 3.2 (one behind the latest) and I am compiling with
-pedantic and -std=c++11
I expect my test to NOT compile yet it compiles and runs.
int myArray[ func_returning_random_int_definitely_not_constexpr( ) ];
Is this a compiler bug or an I missing something?
In response to the comment here is the random_int_function()
#include <random>
int random_int_function(int i)
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,100);
int random_int = distribution(generator);
return i + random_int;
}
Yes, variable length arrays are supported in clang 3.2/3.3 contrary to
the C++11 Standard (§ 8.3.4/1).
So as you say, a program such as:
#include <random>
int random_int_function(int i)
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,100);
int random_int = distribution(generator);
return i + random_int;
}
int main() {
int myArray[ random_int_function( 0 ) ];
(void)myArray;
return 0;
}
compiles and runs. However, with the options -pedantic; -std=c++11 that
you say you passed, clang 3.2/3,3 diagnoses:
warning: variable length arrays are a C99 feature [-Wvla]
The behaviour matches that of gcc (4.7.2/4.8.1), which warns more emphatically:
warning: ISO C++ forbids variable length array ‘myArray’ [-Wvla]
To make the diagnostic be an error, for either compiler, pass -Werror=vla.
Simply plugging the snippets you posted into IDEone, without putting the array declaration into a function, I get
prog.cpp:12:39: error: array bound is not an integer constant before ‘]’ token
Adding a main() function around it results in success, as you observed.
Since C++11 doesn't allow for array declarations that are legal in main but not namespace scope, and that is a property of VLAs, it is reasonable to conclude that is what you are seeing.
Update: courtesy Coliru.org, the message from Clang is
main.cpp:12:9: error: variable length array declaration not allowed at file scope
So that's fairly definite.
Use these options:
-Wvla to warning vla uses
-Werror=vla to consider vla an error.
This works in both the clang and gcc compilers.
Related
struct Foo {
char a[10];
int b;
};
static Foo foo = {.a="bla"};
Compiling the above code gives the following gcc error:
$ gcc -std=gnu++2a test.cpp
C99 designator ‘a’ outside aggregate initializer
I thought that c-string designators in initializer list like these are ok in C++20? What am I missing? I am using gcc version 10.
This is a known bug with GCC: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55227
Unfortunately, you will have to either not use designated initializers or use a different initializer for the array:
static Foo foo = {"bla"};
static Foo foo = {.a={'b', 'l', 'a', 0}};
I got the same error and I dealt with mv my.cpp my.c
You also can find answer in this link:
https://pcbartists.com/firmware/esp32-firmware/designator-outside-aggregate-initializer-solved/
#ifdef __cplusplus
extern "C"
{
#endif
// C code goes here
#ifdef __cplusplus
}
#endif
I use strncpy (from <cstring>), for example:
strncpy(foo.str, "hello", sizeof(foo_t::str));
It seems to be optimized away, therefore generating the same assembly as normal approach in newer GCC (11.3^) and the same as using Artyer solution (the accepted one) of using char array.
Godbolt link: https://godbolt.org/z/9G7b6PT9b
However, the solution might cause warning: missing initializer for member 'foo_t::str' [-Wmissing-field-initializers] if you have the -Wextra warnings enable, but you can use -Wno-missing-field-initializers to exclude it.
By the way, with stucts like that you always have to remember the space is limited, and in many cases you might want leave trailing zero for string end. Using strncpy you can force that by adding:
foo.str[sizeof(foo_t::str) - 1] = 0;
When I use the time() function (i.e., just randomize seed for rand() ) but not include the header file time.h, it works for C. For example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
srand(time(NULL));
for(i=0;i<10;i++){
printf("\t%d",rand()%10);
}
printf("\n");
return 0;
}
When I try to compile the code above, g++ cannot compile it since time.h isn't included. But gcc can.
$gcc ra.c
$./a.out
4 5 2 4 8 7 3 8 9 3
$g++ ra.c
ra.c: In function ‘int main()’:
ra.c:8:20: error: ‘time’ was not declared in this scope
srand(time(NULL));
^
Is it related with version of gcc or just a difference between C/C++ ?
You should include <time.h> for time(2) and turn on the warnings. In C, a function with no visible prototype is assumed to return int (which has been deprecated since C99). So compiling with gcc seems fine while g++ doesn't.
Compile with:
gcc -Wall -Wextra -std=c99 -pedantic-errors file.c
and you'll see gcc also complains about it.
C89/C90 (commonly, but incorrectly, referred to as "ANSI C") had an "implicit int" rule. If you called a function with no visible declaration, the compiler would effectively create an implicit declaration assuming that the function takes arguments of the types that appear in the call and returns int.
The time function takes an argument of type time_t* and returns a value of type time_t. So given a call
time(NULL)
with no visible declaration, the compiler will generate code as if it took an argument of the type of NULL (which is likely to be int) and returns an int result. Given
srand(time(NULL))
the value returned by time(NULL) will then be implicitly converted from int to the `unsig
If int, time_t, and time_t* all happen to be, say, 32 bits the call is likely to work. If they're of different sizes,
For the following program, I do not get a warning that uoff.Reg and s.i is used without initialization. gcc (with -Wextra) and clang (with -Weverything) both do not warn, as I expected.
#include<stdint.h>
typedef union {
uint32_t Reg;
struct {
uint16_t Cx;
uint16_t sf;
};
} tf;
typedef struct {
uint16_t i;
uint16_t j;
} st;
int bar(tf, tf);
int foo(tf t0, int32_t offset) {
tf uoff;
st s;
t0.Reg = uoff.Reg + s.i + (uint32_t)offset;
t0.sf = uoff.sf;
return bar(t0, uoff);
}
I'm wondering why I'm not getting any warning. Does standard (C/C++) say that aggregates with automatic storage are initialized (by default) to zero, or is it a compiler limitation?
I checked gcc, : http://coliru.stacked-crooked.com/a/f1b25f7f369fbdbc, and warnings does get generated
here is how it gets compiled:
g++-4.8 -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
and output:
main.cpp:10:3: warning: ISO C++ prohibits anonymous structs [-Wpedantic]
};
^
main.cpp: In function 'int foo(tf, int32_t)':
main.cpp:23:21: warning: 'uoff.tf::Reg' is used uninitialized in this function -Wuninitialized]
t0.Reg = uoff.Reg + s.i + (uint32_t)offset;
^
main.cpp:23:21: warning: 's.st::i' is used uninitialized in this function -Wuninitialized]
/tmp/ccIy8fGt.o: In function `foo(tf, int)':
main.cpp:(.text+0xa): undefined reference to `bar(tf, tf)'
collect2: error: ld returned 1 exit status
In order to guarantee zero-initialization of POD aggregate data members, you need to value-initialize your instance. For example,
st s = st(); // C++03 and C++11
st s = {}; // C++03 and C++11
st s{}; // C++11
Compilers are not required to warn you about uninitializad variables. However, the minimal set of compilation flags needed to get a warning with g++ seem to be
-Wuninitialized -O2
In other words, it seems that without optimization, the warning disappears. It could be that in this case variables get zero initialized.
Does standard (C/C++) say that aggregates with automatic storage are
initialized (by default) to zero
No, it doesn't. Automatic variables are not initialized, their value is initially left undefined. Using them without initialization results in undefined behavior.
However, naturally, the standard doesn't require you to initialize variables, compilers are free to warn about this, but using an uninitialized variable is not a constraint violation, thus, implementations do not have to warn you.
No, the C-Standard does not require the compiler to generate code to initialise auto variables. Reading their value without prior initialisation provokes undefined behaviuor.
However, this code
int main(void)
{
int a;
int b = a;
b = b;
return 0;
}
compiled using gcc's option -Wall, gives:
main.c:4: warning: ‘a’ is used uninitialized in this function
[gcc (Debian 4.4.5-8) 4.4.5]
Is it possible to see what is going on behind gcc and g++ compilation process?
I have the following program:
#include <stdio.h>
#include <unistd.h>
size_t sym1 = 100;
size_t *addr = &sym1;
size_t *arr = (size_t*)((size_t)&arr + (size_t)&addr);
int main (int argc, char **argv)
{
(void) argc;
(void) argv;
printf("libtest: addr of main(): %p\n", &main);
printf("libtest: addr of arr: %p\n", &arr);
while(1);
return 0;
}
Why is it possible to produce the binary without error with g++ while there is an error using gcc?
I'm looking for a method to trace what makes them behave differently.
# gcc test.c -o test_app
test.c:7:1: error: initializer element is not constant
# g++ test.c -o test_app
I think the reason can be in fact that gcc uses cc1 as a compiler and g++ uses cc1plus.
Is there a way to make more precise output of what actually has been done?
I've tried to use -v flag but the output is quite similar. Are there different flags passed to linker?
What is the easiest way to compare two compilation procedures and find the difference in them?
In this case, gcc produces nothing because your program is not valid C. As the compiler explains, the initializer element (expression used to initialize the global variable arr) is not constant.
C requires initialization expressions to be compile-time constants, so that the contents of local variables can be placed in the data segment of the executable. This cannot be done for arr because the addresses of variables involved are not known until link time and their sum cannot be trivially filled in by the dynamic linker, as is the case for addr1. C++ allows this, so g++ generates initialization code that evaluates the non-constant expressions and stores them in global variables. This code is executed before invocation of main().
Executables cc1 and cc1plus are internal details of the implementation of the compiler, and as such irrelevant to the observed behavior. The relevant fact is that gcc expects valid C code as its input, and g++ expects valid C++ code. The code you provided is valid C++, but not valid C, which is why g++ compiles it and gcc doesn't.
There is a slightly more interesting question lurking here. Consider the following test cases:
#include <stdint.h>
#if TEST==1
void *p=(void *)(unsigned short)&p;
#elif TEST==2
void *p=(void *)(uintptr_t)&p;
#elif TEST==3
void *p=(void *)(1*(uintptr_t)&p);
#elif TEST==4
void *p=(void *)(2*(uintptr_t)&p);
#endif
gcc (even with the very conservative flags -ansi -pedantic-errors) rejects test 1 but accepts test 2, and accepts test 3 but rejects test 4.
From this I conclude that some operations that are easily optimized away (like casting to an object of the same size, or multiplying by 1) get eliminated before the check for whether the initializer is a constant expression.
So gcc might be accepting a few things that it should reject according to the C standard. But when you make them slightly more complicated (like adding the result of a cast to the result of another cast - what useful value can possibly result from adding two addresses anyway?) it notices the problem and rejects the expression.
So I was talking to my friend, helping her with a piece of code, and I always thought that arrays needed to be compile-time constants, as they are on the stack. But she said that her friend did this using this code:
#include <iostream.h>
#include <stdlib.h>
int main()
{
int value = ' ' ;
int sum = 0;
int count = 0;
cout<<"Please enter the total number of employees" <<endl;;
cin>> value;
int numbers[value];
cout<<"Now enter the employees corresponding salaries" <<endl;;
for (int k = 0; k < value; k++)
{
cin >> numbers[k];
}
}
They are using Dev-C++.
Is this code suppose to work? I assume not.
Variable-length arrays are an extension in gcc and g++ ... so this won't work in every compiler.
For more information on gcc's support for variable length arrays, you can see the documentation here.
I believe that variable length arrays are officially unsupported in C++ but certain compilers and/or language extensions implement them.
If you want a variable length array I recommend using std::vector.
You can view its reference here:
http://www.cplusplus.com/reference/stl/vector/
#include <iostream.h>
^ is not a standard header. It used to be there in pre-standard times, i.e. before 1998. It's not there in e.g. modern Visual C++.
cin>> value;
int numbers[value];
Variable Length Arrays, or VLAs, were introduced in C99, a year after C++ was standardized. So they were not part of original standard C++, and happily they were not adopted in C++11 either. Instead of such beast, use e.g. std::vector from the vector header, or some other standard library container.
g++ supports variable length arrays as a language extension. You'd better turn off such extension. E.g.,
d:\dev\test> g++ foo.cpp
d:\dev\test> g++ -pedantic -std=c++0x -Wall -O foo.cpp
foo.cpp: In function 'int main()':
foo.cpp:11: warning: ISO C++ forbids variable length array 'numbers'
foo.cpp:7: warning: unused variable 'sum'
foo.cpp:8: warning: unused variable 'count'
d:\dev\test> _
I tried compiling it using GCC 4.6, and found that the code you've posted compiled successfully. I also tried running it and found that it worked, but I don't think the code is very good.