This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is this self initialization valid?
Is this a well-defined C/C++ program or not?
int foo = foo;
int main()
{
}
Would foo be zero-initialized, or is it undefined behaviour?
It is an ill-formed C program. In C initializers for objects with static storage duration must be constant expressions. The foo on the right-hand side is not a constant expression.
In C++ it is well-formed and has defined behavior, because of zero-initialization of objects with static storage duration (which takes place before any other initialization).
Static/global variables are initialized with 0. Thus:
int ThisIsZero;
int main(void)
{
static int AndSoIsThis;
int ButThisIsNotInitialized;
...
};
It doesn't even compile in C. You cannot initialize global variables other than using compile time constants.
That does not compile - and what whats the point of the question?
Related
This question already has answers here:
Can I overwrite a const object via placement-new?
(2 answers)
Closed 9 months ago.
Is the following code legal according to the standard?
#include <new>
int main() {
const int x = 3;
new ((void *)&x) int { 15 };
}
It seems to me that as long as there is no use of a reference to x it should be valid.
As per the c++ standard basic.life 8:
a pointer that pointed to the original object, a reference that
referred to the original object [...]
the type of the original
object is not const-qualified, and, if a class type, does not contain
any non-static data member whose type is const-qualified or a
reference type
PS : if the answer could contain a reference to the standard it would be much appreciated
Is the following code legal according to the standard?
The behaviour of the program is undefined:
[basic.life]
Creating a new object within the storage that a const complete object with static, thread, or automatic storage duration occupies, or within the storage that such a const object used to occupy before its lifetime ended, results in undefined behavior.
This question already has answers here:
Difference between initialization of static variables in C and C++
(3 answers)
Closed 3 years ago.
#include <stdio.h>
int b(){
return 5;
}
int main(){
static int a = b();
return 0;
}
Above code doesn't compile in C with this error:
error: initializer element is not a compile-time constant
but compiles fine in C++. What are the differences between initializing static values in C and C++?
From cppreference:
Variables declared at block scope with the specifier static or thread_local (since C++11) have static or thread (since C++11) storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.
So, in C static is initialized at startup, while in C++ during the first time the code passes through this section of code.
This will allow assignment of a return from a function in C++ which would be impossible in C since C would need to know the value before the program starts to run...
I hope this helps
Lior
I accidentally created a bug in a program by self-referencing in an array. Here's a very simplified demo program similar in concept:
#include <iostream>
using namespace std;
int kTest[] = {
kTest[0]
};
int main() {
cout << kTest[0] << endl;
}
I was surprised that I received neither a compiler error or even a warning on this code! In my case it ended up producing unpredictable output. Is it accessing garbage memory?
I was curious about under what circumstances this would have well-defined output (if ever!).
Edit: Does it make a difference if kTest is static? What about const? Both?
int kTest[] = {
kTest[0]
};
is similar to, if not exactly same as
int x = x;
It will be undefined behavior, if declared locally in a function.
It seems to be well defined when kTest is a global variable. See the other answer for additional details.
I'm not so sure this is undefined. Quote from the current draft:
[basic.start.static]/3
If constant initialization is not performed, a variable with static
storage duration ([basic.stc.static]) or thread storage duration
([basic.stc.thread]) is zero-initialized ([dcl.init]). Together,
zero-initialization and constant initialization are called static
initialization; all other initialization is dynamic initialization.
Static initialization shall be performed before any dynamic initialization takes place.
To me it looks like kTest is already zero-initialized when the dynamic initialization starts, so it may be defined to initialize to 0.
This question already has answers here:
Why can't the size of a static array be made variable?
(4 answers)
Closed 8 years ago.
I am compiling my C code using the gcc compiler
My code is
main()
{
int i;
scanf("%d",&i);
int a[i];
}
... and it executes it without any warning. However, if I use:
main()
{
int i;
scanf("%d",&i);
static int a[i];
}
... I get an error message saying the size of array is not constant.
If execution of program starts at main function it should not give such error.
and if static data is allocated first before start of main(), how is the compiler able to generate such errors during during compile time?
Variables length arrays are not allowed to be static from the draft C99 standard section 6.7.5.2 Array declarators paragraph 2 says (emphasis mine):
An ordinary identifier (as defined in 6.2.3) that has a variably modified type shall have
either block scope and no linkage or function prototype scope. If an identifier is declared to be an object with static storage duration, it shall not have a variable length array type.
In C++ variable length arrays are not part of the standard and are a compiler extension but gcc should be be following the C99 rules there as well.
This question already has answers here:
Default variable value
(10 answers)
Closed 9 years ago.
Every variable should be properly defined and initialized(assign a value to it) before being used. However under some circumstances, c++ will set variables with a default value of zero. Like the case below.
class A{
...
static int val;
...};
//int val = 10; //This is the usual definition.
int val;//Definition without assigning a value.
...
A a; //a class A object
std::cout<<a.val;
The result would be zero. Obviously, the compiler did something to initialize variable a.val to zero. I am curious about when will they do this generally?
http://en.cppreference.com/w/cpp/language/zero_initialization
Zero initialization is performed in the following situations:
For every named variable with static or thread-local storage duration, before any other initialization.
As part of value-initialization (i.e. with an empty pair of parentheses or braces) sequence for non-class types and for members of value-initialized class types that have no constructors.
When a character array is initialized with a string literal that is too short, the remainder of the array is zero-initialized.