int x; int y; int *ptr; is not initialization, right?
Clash Royale CLAN TAG#URR8PPP
up vote
18
down vote
favorite
I'm reading 'C++ All-in-One for Dummies' by J. P. Mueller and J. Cogswell and stumbled onto this:
#include <iostream>
using namespace std;
int main()
{
int ExpensiveComputer;
int CheapComputer;
int *ptrToComp;
...
This code starts out by initializing all the goodies involved — two integers
and a pointer to an integer.
Just to confirm, this is a mistake and should read '... by declaring', right? It's just strange to me that such basic mistakes still make their way to books.
c++ initialization language-lawyer declaration terminology
New contributor
|
show 1 more comment
up vote
18
down vote
favorite
I'm reading 'C++ All-in-One for Dummies' by J. P. Mueller and J. Cogswell and stumbled onto this:
#include <iostream>
using namespace std;
int main()
{
int ExpensiveComputer;
int CheapComputer;
int *ptrToComp;
...
This code starts out by initializing all the goodies involved — two integers
and a pointer to an integer.
Just to confirm, this is a mistake and should read '... by declaring', right? It's just strange to me that such basic mistakes still make their way to books.
c++ initialization language-lawyer declaration terminology
New contributor
4
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
Nov 20 at 11:11
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
Nov 20 at 11:11
6
If you choose a book for dummies, what do you expect?
– molbdnilo
Nov 20 at 11:17
8
@molbdnilo, a clear and easy-to-understand explanation that's factually correct, though. :) Writing 'for dummies' books does not justify factual mistakes.
– John Allison
Nov 20 at 11:31
1
Re: should read '... by declaring'" -- that's defining. These three statements create three variables. They are definitions. A definition is also a declaration, but a declaration is not a definition.
– Pete Becker
Nov 20 at 15:35
|
show 1 more comment
up vote
18
down vote
favorite
up vote
18
down vote
favorite
I'm reading 'C++ All-in-One for Dummies' by J. P. Mueller and J. Cogswell and stumbled onto this:
#include <iostream>
using namespace std;
int main()
{
int ExpensiveComputer;
int CheapComputer;
int *ptrToComp;
...
This code starts out by initializing all the goodies involved — two integers
and a pointer to an integer.
Just to confirm, this is a mistake and should read '... by declaring', right? It's just strange to me that such basic mistakes still make their way to books.
c++ initialization language-lawyer declaration terminology
New contributor
I'm reading 'C++ All-in-One for Dummies' by J. P. Mueller and J. Cogswell and stumbled onto this:
#include <iostream>
using namespace std;
int main()
{
int ExpensiveComputer;
int CheapComputer;
int *ptrToComp;
...
This code starts out by initializing all the goodies involved — two integers
and a pointer to an integer.
Just to confirm, this is a mistake and should read '... by declaring', right? It's just strange to me that such basic mistakes still make their way to books.
c++ initialization language-lawyer declaration terminology
c++ initialization language-lawyer declaration terminology
New contributor
New contributor
edited Nov 20 at 11:56
New contributor
asked Nov 20 at 11:09
John Allison
12411
12411
New contributor
New contributor
4
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
Nov 20 at 11:11
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
Nov 20 at 11:11
6
If you choose a book for dummies, what do you expect?
– molbdnilo
Nov 20 at 11:17
8
@molbdnilo, a clear and easy-to-understand explanation that's factually correct, though. :) Writing 'for dummies' books does not justify factual mistakes.
– John Allison
Nov 20 at 11:31
1
Re: should read '... by declaring'" -- that's defining. These three statements create three variables. They are definitions. A definition is also a declaration, but a declaration is not a definition.
– Pete Becker
Nov 20 at 15:35
|
show 1 more comment
4
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
Nov 20 at 11:11
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
Nov 20 at 11:11
6
If you choose a book for dummies, what do you expect?
– molbdnilo
Nov 20 at 11:17
8
@molbdnilo, a clear and easy-to-understand explanation that's factually correct, though. :) Writing 'for dummies' books does not justify factual mistakes.
– John Allison
Nov 20 at 11:31
1
Re: should read '... by declaring'" -- that's defining. These three statements create three variables. They are definitions. A definition is also a declaration, but a declaration is not a definition.
– Pete Becker
Nov 20 at 15:35
4
4
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
Nov 20 at 11:11
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
Nov 20 at 11:11
4
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
Nov 20 at 11:11
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
Nov 20 at 11:11
6
6
If you choose a book for dummies, what do you expect?
– molbdnilo
Nov 20 at 11:17
If you choose a book for dummies, what do you expect?
– molbdnilo
Nov 20 at 11:17
8
8
@molbdnilo, a clear and easy-to-understand explanation that's factually correct, though. :) Writing 'for dummies' books does not justify factual mistakes.
– John Allison
Nov 20 at 11:31
@molbdnilo, a clear and easy-to-understand explanation that's factually correct, though. :) Writing 'for dummies' books does not justify factual mistakes.
– John Allison
Nov 20 at 11:31
1
1
Re: should read '... by declaring'" -- that's defining. These three statements create three variables. They are definitions. A definition is also a declaration, but a declaration is not a definition.
– Pete Becker
Nov 20 at 15:35
Re: should read '... by declaring'" -- that's defining. These three statements create three variables. They are definitions. A definition is also a declaration, but a declaration is not a definition.
– Pete Becker
Nov 20 at 15:35
|
show 1 more comment
4 Answers
4
active
oldest
votes
up vote
22
down vote
accepted
From the point of view of the language, this is default initialization. The problem is, they are initialized to indeterminate values.
otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)
Note that any attempt to read these indeterminate values leads to UB.
From the standard, [dcl.init]/7
To default-initialize an object of type T means:
If T is a (possibly cv-qualified) class type ([class]), constructors are considered. The applicable constructors are enumerated
([over.match.ctor]), and the best one for the initializer () is chosen
through overload resolution ([over.match]). The constructor thus
selected is called, with an empty argument list, to initialize the
object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
1
There is a crucial difference between initialized with an indeterminate value and not initializated: the former requires a memory store, the latter does not.
– Maxim Egorushkin
Nov 20 at 11:34
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
Nov 20 at 11:39
Not sure which expression you refer to.
– Maxim Egorushkin
Nov 20 at 11:41
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
Nov 20 at 11:42
4
@MaximEgorushkin -- that may be formally true, but you cannot write a conforming program that can tell whether a memory store occurred in these cases, so the as-if rule says that they're the same thing.
– Pete Becker
Nov 20 at 15:33
|
show 3 more comments
up vote
6
down vote
Yes you are correct.
You declared and defined these variables, you did not initialize them!
PS: What is the difference between a definition and a declaration?
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
Nov 20 at 11:19
@FantasticMrFox, yes, I read that before posting my question. It was just weird to me that the authors, having such experience (for what it's worth), could write something that's so trivially wrong. Having read songyuanyao 's answer, it's not so trivial, though.
– John Allison
Nov 20 at 11:25
FantasticMrFox you are right, updated. @JohnAllison I am not sure, it they wanted to be so on point, they would say default initialize with garbage/random values. Saying initialize a variable and not doing so is so misleading and wrong. I see what extra information songyuanyao's answer brought to the table, but it's not a boolean one. Anyway, glad you found an answer that fits your thoughts.
– gsamaras
Nov 20 at 14:29
add a comment |
up vote
3
down vote
This code both declares and defines three variables but does not initialize them (their values are said to be indeterminate).
A variable declaration only must include keyword extern
.
add a comment |
up vote
1
down vote
Right. Hence, "dummies". :)
We can't even blame this on legacy; historically C programmers would declare* a variable and then "initialize" it later with its first assignment.
But it was never the case that simply declaring a variable, without an initializer, were deemed to be "initializing" it.**
So the wording is just wrong.
* Technically we're talking about definitions, but when we say "declare a variable" we almost always mean defining declarations.
** Though objects with static storage duration do undergo their own zero-initialisation phase before anything else happens, so forgoing initialisation yourself is not a catastrophe in that case. Still, we cannot claim that we have initialised that object.
2
A variable declaration only must include keywordextern
, otherwise it is a definition. The initializer is a separate concern.
– Maxim Egorushkin
Nov 20 at 11:47
1
@MaximEgorushkin Definitions are declarations too. But, sure, we can say "simply defining a thing" too and the lesson stays the same. Not really the key point, is it?
– Lightness Races in Orbit
Nov 20 at 11:53
1
In a declaration only you can write, for example,extern int a;
(no array bounds), unlike in a definition. Confusing the two never helps.
– Maxim Egorushkin
Nov 20 at 12:10
2
But it was never the case that simply declaring a thing without an initializer were deemed "initializing" it. - Except when it is static or global.
– Fantastic Mr Fox
Nov 20 at 14:01
2
@MaximEgorushkin Way off-topic and why not? Why is it important to you? Why does gsamaras use a picture of an airplane as their profile picture?
– Lightness Races in Orbit
Nov 20 at 14:35
|
show 8 more comments
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
22
down vote
accepted
From the point of view of the language, this is default initialization. The problem is, they are initialized to indeterminate values.
otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)
Note that any attempt to read these indeterminate values leads to UB.
From the standard, [dcl.init]/7
To default-initialize an object of type T means:
If T is a (possibly cv-qualified) class type ([class]), constructors are considered. The applicable constructors are enumerated
([over.match.ctor]), and the best one for the initializer () is chosen
through overload resolution ([over.match]). The constructor thus
selected is called, with an empty argument list, to initialize the
object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
1
There is a crucial difference between initialized with an indeterminate value and not initializated: the former requires a memory store, the latter does not.
– Maxim Egorushkin
Nov 20 at 11:34
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
Nov 20 at 11:39
Not sure which expression you refer to.
– Maxim Egorushkin
Nov 20 at 11:41
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
Nov 20 at 11:42
4
@MaximEgorushkin -- that may be formally true, but you cannot write a conforming program that can tell whether a memory store occurred in these cases, so the as-if rule says that they're the same thing.
– Pete Becker
Nov 20 at 15:33
|
show 3 more comments
up vote
22
down vote
accepted
From the point of view of the language, this is default initialization. The problem is, they are initialized to indeterminate values.
otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)
Note that any attempt to read these indeterminate values leads to UB.
From the standard, [dcl.init]/7
To default-initialize an object of type T means:
If T is a (possibly cv-qualified) class type ([class]), constructors are considered. The applicable constructors are enumerated
([over.match.ctor]), and the best one for the initializer () is chosen
through overload resolution ([over.match]). The constructor thus
selected is called, with an empty argument list, to initialize the
object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
1
There is a crucial difference between initialized with an indeterminate value and not initializated: the former requires a memory store, the latter does not.
– Maxim Egorushkin
Nov 20 at 11:34
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
Nov 20 at 11:39
Not sure which expression you refer to.
– Maxim Egorushkin
Nov 20 at 11:41
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
Nov 20 at 11:42
4
@MaximEgorushkin -- that may be formally true, but you cannot write a conforming program that can tell whether a memory store occurred in these cases, so the as-if rule says that they're the same thing.
– Pete Becker
Nov 20 at 15:33
|
show 3 more comments
up vote
22
down vote
accepted
up vote
22
down vote
accepted
From the point of view of the language, this is default initialization. The problem is, they are initialized to indeterminate values.
otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)
Note that any attempt to read these indeterminate values leads to UB.
From the standard, [dcl.init]/7
To default-initialize an object of type T means:
If T is a (possibly cv-qualified) class type ([class]), constructors are considered. The applicable constructors are enumerated
([over.match.ctor]), and the best one for the initializer () is chosen
through overload resolution ([over.match]). The constructor thus
selected is called, with an empty argument list, to initialize the
object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
From the point of view of the language, this is default initialization. The problem is, they are initialized to indeterminate values.
otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
Default initialization of non-class variables with automatic and dynamic storage duration produces objects with indeterminate values (static and thread-local objects get zero initialized)
Note that any attempt to read these indeterminate values leads to UB.
From the standard, [dcl.init]/7
To default-initialize an object of type T means:
If T is a (possibly cv-qualified) class type ([class]), constructors are considered. The applicable constructors are enumerated
([over.match.ctor]), and the best one for the initializer () is chosen
through overload resolution ([over.match]). The constructor thus
selected is called, with an empty argument list, to initialize the
object.
If T is an array type, each element is default-initialized.
Otherwise, no initialization is performed.
edited Nov 20 at 11:51
answered Nov 20 at 11:21
songyuanyao
88.5k11170232
88.5k11170232
1
There is a crucial difference between initialized with an indeterminate value and not initializated: the former requires a memory store, the latter does not.
– Maxim Egorushkin
Nov 20 at 11:34
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
Nov 20 at 11:39
Not sure which expression you refer to.
– Maxim Egorushkin
Nov 20 at 11:41
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
Nov 20 at 11:42
4
@MaximEgorushkin -- that may be formally true, but you cannot write a conforming program that can tell whether a memory store occurred in these cases, so the as-if rule says that they're the same thing.
– Pete Becker
Nov 20 at 15:33
|
show 3 more comments
1
There is a crucial difference between initialized with an indeterminate value and not initializated: the former requires a memory store, the latter does not.
– Maxim Egorushkin
Nov 20 at 11:34
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
Nov 20 at 11:39
Not sure which expression you refer to.
– Maxim Egorushkin
Nov 20 at 11:41
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
Nov 20 at 11:42
4
@MaximEgorushkin -- that may be formally true, but you cannot write a conforming program that can tell whether a memory store occurred in these cases, so the as-if rule says that they're the same thing.
– Pete Becker
Nov 20 at 15:33
1
1
There is a crucial difference between initialized with an indeterminate value and not initializated: the former requires a memory store, the latter does not.
– Maxim Egorushkin
Nov 20 at 11:34
There is a crucial difference between initialized with an indeterminate value and not initializated: the former requires a memory store, the latter does not.
– Maxim Egorushkin
Nov 20 at 11:34
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
Nov 20 at 11:39
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
Nov 20 at 11:39
Not sure which expression you refer to.
– Maxim Egorushkin
Nov 20 at 11:41
Not sure which expression you refer to.
– Maxim Egorushkin
Nov 20 at 11:41
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
Nov 20 at 11:42
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
Nov 20 at 11:42
4
4
@MaximEgorushkin -- that may be formally true, but you cannot write a conforming program that can tell whether a memory store occurred in these cases, so the as-if rule says that they're the same thing.
– Pete Becker
Nov 20 at 15:33
@MaximEgorushkin -- that may be formally true, but you cannot write a conforming program that can tell whether a memory store occurred in these cases, so the as-if rule says that they're the same thing.
– Pete Becker
Nov 20 at 15:33
|
show 3 more comments
up vote
6
down vote
Yes you are correct.
You declared and defined these variables, you did not initialize them!
PS: What is the difference between a definition and a declaration?
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
Nov 20 at 11:19
@FantasticMrFox, yes, I read that before posting my question. It was just weird to me that the authors, having such experience (for what it's worth), could write something that's so trivially wrong. Having read songyuanyao 's answer, it's not so trivial, though.
– John Allison
Nov 20 at 11:25
FantasticMrFox you are right, updated. @JohnAllison I am not sure, it they wanted to be so on point, they would say default initialize with garbage/random values. Saying initialize a variable and not doing so is so misleading and wrong. I see what extra information songyuanyao's answer brought to the table, but it's not a boolean one. Anyway, glad you found an answer that fits your thoughts.
– gsamaras
Nov 20 at 14:29
add a comment |
up vote
6
down vote
Yes you are correct.
You declared and defined these variables, you did not initialize them!
PS: What is the difference between a definition and a declaration?
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
Nov 20 at 11:19
@FantasticMrFox, yes, I read that before posting my question. It was just weird to me that the authors, having such experience (for what it's worth), could write something that's so trivially wrong. Having read songyuanyao 's answer, it's not so trivial, though.
– John Allison
Nov 20 at 11:25
FantasticMrFox you are right, updated. @JohnAllison I am not sure, it they wanted to be so on point, they would say default initialize with garbage/random values. Saying initialize a variable and not doing so is so misleading and wrong. I see what extra information songyuanyao's answer brought to the table, but it's not a boolean one. Anyway, glad you found an answer that fits your thoughts.
– gsamaras
Nov 20 at 14:29
add a comment |
up vote
6
down vote
up vote
6
down vote
Yes you are correct.
You declared and defined these variables, you did not initialize them!
PS: What is the difference between a definition and a declaration?
Yes you are correct.
You declared and defined these variables, you did not initialize them!
PS: What is the difference between a definition and a declaration?
edited Nov 20 at 13:18
answered Nov 20 at 11:13
gsamaras
48k2394174
48k2394174
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
Nov 20 at 11:19
@FantasticMrFox, yes, I read that before posting my question. It was just weird to me that the authors, having such experience (for what it's worth), could write something that's so trivially wrong. Having read songyuanyao 's answer, it's not so trivial, though.
– John Allison
Nov 20 at 11:25
FantasticMrFox you are right, updated. @JohnAllison I am not sure, it they wanted to be so on point, they would say default initialize with garbage/random values. Saying initialize a variable and not doing so is so misleading and wrong. I see what extra information songyuanyao's answer brought to the table, but it's not a boolean one. Anyway, glad you found an answer that fits your thoughts.
– gsamaras
Nov 20 at 14:29
add a comment |
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
Nov 20 at 11:19
@FantasticMrFox, yes, I read that before posting my question. It was just weird to me that the authors, having such experience (for what it's worth), could write something that's so trivially wrong. Having read songyuanyao 's answer, it's not so trivial, though.
– John Allison
Nov 20 at 11:25
FantasticMrFox you are right, updated. @JohnAllison I am not sure, it they wanted to be so on point, they would say default initialize with garbage/random values. Saying initialize a variable and not doing so is so misleading and wrong. I see what extra information songyuanyao's answer brought to the table, but it's not a boolean one. Anyway, glad you found an answer that fits your thoughts.
– gsamaras
Nov 20 at 14:29
1
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
Nov 20 at 11:19
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
Nov 20 at 11:19
@FantasticMrFox, yes, I read that before posting my question. It was just weird to me that the authors, having such experience (for what it's worth), could write something that's so trivially wrong. Having read songyuanyao 's answer, it's not so trivial, though.
– John Allison
Nov 20 at 11:25
@FantasticMrFox, yes, I read that before posting my question. It was just weird to me that the authors, having such experience (for what it's worth), could write something that's so trivially wrong. Having read songyuanyao 's answer, it's not so trivial, though.
– John Allison
Nov 20 at 11:25
FantasticMrFox you are right, updated. @JohnAllison I am not sure, it they wanted to be so on point, they would say default initialize with garbage/random values. Saying initialize a variable and not doing so is so misleading and wrong. I see what extra information songyuanyao's answer brought to the table, but it's not a boolean one. Anyway, glad you found an answer that fits your thoughts.
– gsamaras
Nov 20 at 14:29
FantasticMrFox you are right, updated. @JohnAllison I am not sure, it they wanted to be so on point, they would say default initialize with garbage/random values. Saying initialize a variable and not doing so is so misleading and wrong. I see what extra information songyuanyao's answer brought to the table, but it's not a boolean one. Anyway, glad you found an answer that fits your thoughts.
– gsamaras
Nov 20 at 14:29
add a comment |
up vote
3
down vote
This code both declares and defines three variables but does not initialize them (their values are said to be indeterminate).
A variable declaration only must include keyword extern
.
add a comment |
up vote
3
down vote
This code both declares and defines three variables but does not initialize them (their values are said to be indeterminate).
A variable declaration only must include keyword extern
.
add a comment |
up vote
3
down vote
up vote
3
down vote
This code both declares and defines three variables but does not initialize them (their values are said to be indeterminate).
A variable declaration only must include keyword extern
.
This code both declares and defines three variables but does not initialize them (their values are said to be indeterminate).
A variable declaration only must include keyword extern
.
edited Nov 20 at 11:32
answered Nov 20 at 11:18
Maxim Egorushkin
83.4k1198179
83.4k1198179
add a comment |
add a comment |
up vote
1
down vote
Right. Hence, "dummies". :)
We can't even blame this on legacy; historically C programmers would declare* a variable and then "initialize" it later with its first assignment.
But it was never the case that simply declaring a variable, without an initializer, were deemed to be "initializing" it.**
So the wording is just wrong.
* Technically we're talking about definitions, but when we say "declare a variable" we almost always mean defining declarations.
** Though objects with static storage duration do undergo their own zero-initialisation phase before anything else happens, so forgoing initialisation yourself is not a catastrophe in that case. Still, we cannot claim that we have initialised that object.
2
A variable declaration only must include keywordextern
, otherwise it is a definition. The initializer is a separate concern.
– Maxim Egorushkin
Nov 20 at 11:47
1
@MaximEgorushkin Definitions are declarations too. But, sure, we can say "simply defining a thing" too and the lesson stays the same. Not really the key point, is it?
– Lightness Races in Orbit
Nov 20 at 11:53
1
In a declaration only you can write, for example,extern int a;
(no array bounds), unlike in a definition. Confusing the two never helps.
– Maxim Egorushkin
Nov 20 at 12:10
2
But it was never the case that simply declaring a thing without an initializer were deemed "initializing" it. - Except when it is static or global.
– Fantastic Mr Fox
Nov 20 at 14:01
2
@MaximEgorushkin Way off-topic and why not? Why is it important to you? Why does gsamaras use a picture of an airplane as their profile picture?
– Lightness Races in Orbit
Nov 20 at 14:35
|
show 8 more comments
up vote
1
down vote
Right. Hence, "dummies". :)
We can't even blame this on legacy; historically C programmers would declare* a variable and then "initialize" it later with its first assignment.
But it was never the case that simply declaring a variable, without an initializer, were deemed to be "initializing" it.**
So the wording is just wrong.
* Technically we're talking about definitions, but when we say "declare a variable" we almost always mean defining declarations.
** Though objects with static storage duration do undergo their own zero-initialisation phase before anything else happens, so forgoing initialisation yourself is not a catastrophe in that case. Still, we cannot claim that we have initialised that object.
2
A variable declaration only must include keywordextern
, otherwise it is a definition. The initializer is a separate concern.
– Maxim Egorushkin
Nov 20 at 11:47
1
@MaximEgorushkin Definitions are declarations too. But, sure, we can say "simply defining a thing" too and the lesson stays the same. Not really the key point, is it?
– Lightness Races in Orbit
Nov 20 at 11:53
1
In a declaration only you can write, for example,extern int a;
(no array bounds), unlike in a definition. Confusing the two never helps.
– Maxim Egorushkin
Nov 20 at 12:10
2
But it was never the case that simply declaring a thing without an initializer were deemed "initializing" it. - Except when it is static or global.
– Fantastic Mr Fox
Nov 20 at 14:01
2
@MaximEgorushkin Way off-topic and why not? Why is it important to you? Why does gsamaras use a picture of an airplane as their profile picture?
– Lightness Races in Orbit
Nov 20 at 14:35
|
show 8 more comments
up vote
1
down vote
up vote
1
down vote
Right. Hence, "dummies". :)
We can't even blame this on legacy; historically C programmers would declare* a variable and then "initialize" it later with its first assignment.
But it was never the case that simply declaring a variable, without an initializer, were deemed to be "initializing" it.**
So the wording is just wrong.
* Technically we're talking about definitions, but when we say "declare a variable" we almost always mean defining declarations.
** Though objects with static storage duration do undergo their own zero-initialisation phase before anything else happens, so forgoing initialisation yourself is not a catastrophe in that case. Still, we cannot claim that we have initialised that object.
Right. Hence, "dummies". :)
We can't even blame this on legacy; historically C programmers would declare* a variable and then "initialize" it later with its first assignment.
But it was never the case that simply declaring a variable, without an initializer, were deemed to be "initializing" it.**
So the wording is just wrong.
* Technically we're talking about definitions, but when we say "declare a variable" we almost always mean defining declarations.
** Though objects with static storage duration do undergo their own zero-initialisation phase before anything else happens, so forgoing initialisation yourself is not a catastrophe in that case. Still, we cannot claim that we have initialised that object.
edited Nov 20 at 14:15
answered Nov 20 at 11:46
Lightness Races in Orbit
278k51445763
278k51445763
2
A variable declaration only must include keywordextern
, otherwise it is a definition. The initializer is a separate concern.
– Maxim Egorushkin
Nov 20 at 11:47
1
@MaximEgorushkin Definitions are declarations too. But, sure, we can say "simply defining a thing" too and the lesson stays the same. Not really the key point, is it?
– Lightness Races in Orbit
Nov 20 at 11:53
1
In a declaration only you can write, for example,extern int a;
(no array bounds), unlike in a definition. Confusing the two never helps.
– Maxim Egorushkin
Nov 20 at 12:10
2
But it was never the case that simply declaring a thing without an initializer were deemed "initializing" it. - Except when it is static or global.
– Fantastic Mr Fox
Nov 20 at 14:01
2
@MaximEgorushkin Way off-topic and why not? Why is it important to you? Why does gsamaras use a picture of an airplane as their profile picture?
– Lightness Races in Orbit
Nov 20 at 14:35
|
show 8 more comments
2
A variable declaration only must include keywordextern
, otherwise it is a definition. The initializer is a separate concern.
– Maxim Egorushkin
Nov 20 at 11:47
1
@MaximEgorushkin Definitions are declarations too. But, sure, we can say "simply defining a thing" too and the lesson stays the same. Not really the key point, is it?
– Lightness Races in Orbit
Nov 20 at 11:53
1
In a declaration only you can write, for example,extern int a;
(no array bounds), unlike in a definition. Confusing the two never helps.
– Maxim Egorushkin
Nov 20 at 12:10
2
But it was never the case that simply declaring a thing without an initializer were deemed "initializing" it. - Except when it is static or global.
– Fantastic Mr Fox
Nov 20 at 14:01
2
@MaximEgorushkin Way off-topic and why not? Why is it important to you? Why does gsamaras use a picture of an airplane as their profile picture?
– Lightness Races in Orbit
Nov 20 at 14:35
2
2
A variable declaration only must include keyword
extern
, otherwise it is a definition. The initializer is a separate concern.– Maxim Egorushkin
Nov 20 at 11:47
A variable declaration only must include keyword
extern
, otherwise it is a definition. The initializer is a separate concern.– Maxim Egorushkin
Nov 20 at 11:47
1
1
@MaximEgorushkin Definitions are declarations too. But, sure, we can say "simply defining a thing" too and the lesson stays the same. Not really the key point, is it?
– Lightness Races in Orbit
Nov 20 at 11:53
@MaximEgorushkin Definitions are declarations too. But, sure, we can say "simply defining a thing" too and the lesson stays the same. Not really the key point, is it?
– Lightness Races in Orbit
Nov 20 at 11:53
1
1
In a declaration only you can write, for example,
extern int a;
(no array bounds), unlike in a definition. Confusing the two never helps.– Maxim Egorushkin
Nov 20 at 12:10
In a declaration only you can write, for example,
extern int a;
(no array bounds), unlike in a definition. Confusing the two never helps.– Maxim Egorushkin
Nov 20 at 12:10
2
2
But it was never the case that simply declaring a thing without an initializer were deemed "initializing" it. - Except when it is static or global.
– Fantastic Mr Fox
Nov 20 at 14:01
But it was never the case that simply declaring a thing without an initializer were deemed "initializing" it. - Except when it is static or global.
– Fantastic Mr Fox
Nov 20 at 14:01
2
2
@MaximEgorushkin Way off-topic and why not? Why is it important to you? Why does gsamaras use a picture of an airplane as their profile picture?
– Lightness Races in Orbit
Nov 20 at 14:35
@MaximEgorushkin Way off-topic and why not? Why is it important to you? Why does gsamaras use a picture of an airplane as their profile picture?
– Lightness Races in Orbit
Nov 20 at 14:35
|
show 8 more comments
John Allison is a new contributor. Be nice, and check out our Code of Conduct.
John Allison is a new contributor. Be nice, and check out our Code of Conduct.
John Allison is a new contributor. Be nice, and check out our Code of Conduct.
John Allison is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391694%2fint-x-int-y-int-ptr-is-not-initialization-right%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
Nov 20 at 11:11
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
Nov 20 at 11:11
6
If you choose a book for dummies, what do you expect?
– molbdnilo
Nov 20 at 11:17
8
@molbdnilo, a clear and easy-to-understand explanation that's factually correct, though. :) Writing 'for dummies' books does not justify factual mistakes.
– John Allison
Nov 20 at 11:31
1
Re: should read '... by declaring'" -- that's defining. These three statements create three variables. They are definitions. A definition is also a declaration, but a declaration is not a definition.
– Pete Becker
Nov 20 at 15:35