When I compile and run the following:
#include <iostream>
int main() {
bool f;
std::cout << f << std::endl;
f = not(f);
std::cout << f << std::endl;
}
I get as output:
8
9
Is this according to spec? Am I required to initialize booleans?
Robert 13 20252 rs*******@gmail.com wrote: When I compile and run the following:
#include <iostream>
int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
I get as output: 8 9
Is this according to spec?
Yes, this program has undefined behaviour.
Am I required to initialize booleans?
If you want to avoid something like the above then yes you must
initialize the boolean. Using a uninitialised variable is undefined
behaviour.
john
rswans...@gmail.com wrote: When I compile and run the following:
#include <iostream>
int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
I get as output: 8 9
Is this according to spec? Am I required to initialize booleans?
Robert
Yes. Intrinsic types don't invoke default constructors. You could do
this:
bool f = bool();
But it's better to specify what the default value is.
Cheers! --M
<rs*******@gmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com... When I compile and run the following:
#include <iostream>
int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
I get as output: 8 9
The output could have been that, any other values,
or nothing at all. The program could do *anything*,
because its behavior is undefined. Is this according to spec? Am I required to initialize booleans?
*Any* object of *any* type must have been initialized or
assigned a valid value before its value can be evaluated,
otherwise the behavior is undefined. With standard library
types, initialization happens automatically via a default
constructor. So with a statement such as
std::string s;
it might look as though 's' is not being initialized, but it is
(in this case to an empty string).
Except for types with default constructors (and objects with
static storage duration), you should *always* provide an
initial value when you define them.
-Mike
Uzytkownik <rs*******@gmail.com> napisal w wiadomosci
news:11********************@z14g2000cwz.googlegrou ps.com... When I compile and run the following:
#include <iostream>
int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
I get as output: 8 9
Is this according to spec? Am I required to initialize booleans?
Robert
Yes, You have to initialize booleans if You want to avoid this undefined
bahaviour, but in global range You don't have to do it.
Wojtas
Wojtek is rite..
to add, you dont need to initialize the bool, even if it is defined as
static.
In general, all intrinsic data types are set to 0 when defined under global
scope or when defined static..
Sat
"Wojtek" <ws*****@vp.pl> wrote in message news:dh**********@news.onet.pl... Uzytkownik <rs*******@gmail.com> napisal w wiadomosci news:11********************@z14g2000cwz.googlegrou ps.com... When I compile and run the following:
#include <iostream>
int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
I get as output: 8 9
Is this according to spec? Am I required to initialize booleans?
Robert
Yes, You have to initialize booleans if You want to avoid this undefined bahaviour, but in global range You don't have to do it.
Wojtas
John Harrison wrote: rs*******@gmail.com wrote: When I compile and run the following:
#include <iostream>
int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
I get as output: 8 9
Is this according to spec?
Yes, this program has undefined behaviour.
Am I required to initialize booleans?
If you want to avoid something like the above then yes you must initialize the boolean. Using a uninitialised variable is undefined behaviour.
john
Actually, the value of an uninitialized local variable is merely
indeterminate - meaning that the program's behavior is defined, but
varies depending on whatever the uninitialized value turns out to be.
But once that value is known, the program behaves no differently than
it would have had it explicitly initialized the variable with that
value to begin with. In other words, the program above is just as well
defined as an identical version that had initialized f to 7.
Being uninitialized, the variable's value could turn out to be any
value. This unpredictability is rarely useful or intended, which is why
it is a good idea to initialize local variables. Nevertheless, using an
uninitialized variable does not by itself entail undefined behavior.
Greg
Greg wrote: But once that value is known, the program behaves no differently than it would have had it explicitly initialized the variable with that value to begin with. In other words, the program above is just as well defined as an identical version that had initialized f to 7.
You can't initialize a bool to 7. You can initialize it with the value
7, but that value will be converted to true, and the resulting code will
not act at all like the original code.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
* Greg: Actually, the value of an uninitialized local variable is merely indeterminate - meaning that the program's behavior is defined, but varies depending on whatever the uninitialized value turns out to be.
Nope.
In practice that is the case for built-in integral types, but formally it's
not so. Use of the value of an uninitialized variable is Undefined Behavior,
per §4.1/1. However, there is a footnote somewhere discussing in particular
the use of a 'bool' uninitialized variable, mentioning that it can behave as
if it's neither 'true' nor 'false' -- so the standard isn't entirely clear.
In particular, you get both formal and in-practice UB for non-integral types.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote: * Greg: Actually, the value of an uninitialized local variable is merely indeterminate - meaning that the program's behavior is defined, but varies depending on whatever the uninitialized value turns out to be.
Nope.
In practice that is the case for built-in integral types, but formally it's not so. Use of the value of an uninitialized variable is Undefined Behavior, per §4.1/1. However, there is a footnote somewhere discussing in particular the use of a 'bool' uninitialized variable, mentioning that it can behaveas if it's neither 'true' nor 'false' -- so the standard isn't entirely clear.
In particular, you get both formal and in-practice UB for non-integral types.
§4.1/1 applies to uninitialized objects. §8.5.9 applies to local
variables declared without an initializer. Such variables are
initialized with an "indeterminate" initial value. Granted in the case
of a bool, indeterminate may be neither true or false. But for other
integral types, an uninitialized value is always a defined value - and
accessing it will not result in undefined behavior. The Standard has an
example that makes this point clear [3.3.1]:
int x = x;
The Standard notes that in the above statement x is initialized with
its own "indeterminate" value. Surely, if 4.1/1 applied to local
variables without intializers, this resulut of executing this statement
would have to be undefined behavior. But it is not. The behavior is
merely indeterminate until the value of x is known.
Greg
* Greg: Alf P. Steinbach wrote: * Greg: Actually, the value of an uninitialized local variable is merely indeterminate - meaning that the program's behavior is defined, but varies depending on whatever the uninitialized value turns out to be. Nope.
In practice that is the case for built-in integral types, but formally it=
's not so. Use of the value of an uninitialized variable is Undefined Behav= ior, per =A74.1/1. However, there is a footnote somewhere discussing in parti= cular the use of a 'bool' uninitialized variable, mentioning that it can behave= as if it's neither 'true' nor 'false' -- so the standard isn't entirely cl= ear. In particular, you get both formal and in-practice UB for non-integral ty=
pes.
=A74.1/1 applies to uninitialized objects.
Right.
=A78.5.9 applies to local variables declared without an initializer.
Right.
Such variables are initialized with an "indeterminate" initial value.
Granted in the case of a bool, indeterminate may be neither true or false.
Right.
But for other integral types, an uninitialized value is always a defined value -
In pratice yes, formally no. Even the in-practice has been disputed.
However, such arguments rely on archaic and/or wholly hypothetical computers
with padding bits -- and we don't want to open that can of worms here... ;-)
and accessing it will not result in undefined behavior.
Wrong, see §4.1/1.
The Standard has an example that makes this point clear [3.3.1]:
int x =3D x;
The Standard notes that in the above statement x is initialized with its own "indeterminate" value.
Right.
Surely, if 4.1/1 applied to local variables without intializers, this resulut of executing this statement would have to be undefined behavior.
Right.
But it is not.
Wrong, see §4.1/1.
The behavior is merely indeterminate until the value of x is known.
Wrong, see §4.1/1.
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[converted top-posting to more readable form]
sat wrote: "Wojtek" wrote: Yes, You have to initialize booleans if You want to avoid this undefined bahaviour, but in global range You don't have to do it.
Wojtek is rite.. to add, you dont need to initialize the bool, even if it is defined as static.
In general, all intrinsic data types are set to 0 when defined under global scope or when defined static..
To be more precise, any intrinsic object that is static -- i.e.,
declared static (whether local or at file scope) or is declared in a
namespace (whether global, unnamed, or named) -- is automatically
initialized to 0, converted to the appropriate type. For user-defined
types, the default constructor will be called if it exists, and an
error will result if one does not exist but other constructors do. If
the default constructor was implicitly generated (i.e. no constructors
were defined), data members are default initialized if the object is
static. Consider:
#include <iostream>
using namespace std;
// Note: no default constructor
struct A { int i_; };
// Namespace-scoped object is default initialized
namespace { A a; }
int main()
{
// Local object is not default initialized
A b;
cout << a.i_ << ' ' << b.i_ << endl;
return 0;
}
The result of the cout demonstrates that a.i_ is initialized to 0, but
b.i_ is not (though it may happen to be 0, of course).
IME, however, it is best to make initialization explicit even in the
case of static objects since this is a rather obscure rule and
explicitness here is far from burdensome and makes the intent of the
programmer clear. Also note that the use of static to indicate "local
to this translation unit" is deprecated in C++ in favor of unnamed
namespaces.
Cheers! --M
Wojtek wrote: Uzytkownik <rs*******@gmail.com> napisal w wiadomosci news:11********************@z14g2000cwz.googlegrou ps.com...
When I compile and run the following:
#include <iostream>
int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
I get as output: 8 9
Is this according to spec? Am I required to initialize booleans?
Robert
Yes, You have to initialize booleans if You want to avoid this undefined bahaviour, but in global range You don't have to do it.
Wojtas
But I thought the 0 was false and any non-zero value is true; therefore,
if the uninitialized variable is nonzero and gets complimented, it ends
up zero, and vice versa.
-Steve
you are right upto the part that, non zero values are considered true and
zero is considered false,
but,
there are two kinds of nots or compliment operations:
1. Boolean not .. using the symbol ! b= !b;
2. Binary not.. or negation using the symbol ~ b=~b;
in the first case, a non zero integer would be made zero, and hence false.
but in the second case, all 1 bits are set to 0 and 0 bits are set to 1 in
the integer, resulting in a binary negation,
so if you are using a non zero integer, almost all the time you would end
up with another non zero integer when the binary not is used. (except when
all the bits in the number are 1s)
<sj******@eng.usf.edu> wrote in message news:dh**********@news1.usf.edu... Wojtek wrote: Uzytkownik <rs*******@gmail.com> napisal w wiadomosci news:11********************@z14g2000cwz.googlegrou ps.com...
When I compile and run the following:
#include <iostream>
int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
I get as output: 8 9
Is this according to spec? Am I required to initialize booleans?
Robert
Yes, You have to initialize booleans if You want to avoid this undefined bahaviour, but in global range You don't have to do it.
Wojtas
But I thought the 0 was false and any non-zero value is true; therefore, if the uninitialized variable is nonzero and gets complimented, it ends up zero, and vice versa.
-Steve This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: G Kannan |
last post by:
Hey all!
I have written a perl script to retrieve information from a HTML Form
and insert the data into an Oracle database table. I am gettting the
the following error message:
"Use of...
|
by: rk |
last post by:
Hi,
I'm a beginner for perl/cgi programs and i tried to write a cgi script
and when i ran it, i got the following error. But when i verified it
from the book i typed exactly whatever it is there...
|
by: Liang |
last post by:
Hi,
I use "defined $r_libs->{$name}" to check first if a key exists in a hash
table. But Perl gives a warning WHENEVER the key exists: "Use of
uninitialized value".
Would u please help to...
|
by: Marcin Kalicinski |
last post by:
int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4...
|
by: jyu.james |
last post by:
I'm trying to detect reads of uninitialized global variables (that are
declared in one file, and used in another as an extern).
I know that ANSI C initializes all global variables to 0, however,...
|
by: julien |
last post by:
Hello,
Is it possible if a boolean was initialized or not? For other types of
variable, I usually check if it is null. But this not possible for a
boolean.
Thank you
Julien
|
by: sanjaymeher |
last post by:
Hi,
Right now addDynamicMemory(char **ptr, int size) method can able to
handle if input ptr is intitialized to NULL or something. But how to
improve this method to handle uninitialized pointed...
|
by: pnsreee |
last post by:
Hi All,
I have the following code.
The array @sub_data will contain integers or a string"NO". I have to validate if the array contain integer. If it contain "NO" then no need to validate.
...
|
by: samthemist |
last post by:
When perl comes out with errors like this:
Use of uninitialized value in concatenation (.) or string at NQII.pl line 18, <STDIN> line 2.
Argument "" isn't numeric in numeric ne (!=) at NQII.pl...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |