int x; int y; int *ptr; is not initialization, right?
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
2 days ago
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
2 days ago
6
If you choose a book for dummies, what do you expect?
– molbdnilo
2 days ago
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
2 days ago
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
2 days ago
|
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 2 days ago
New contributor
asked 2 days ago
John Allison
11211
11211
New contributor
New contributor
4
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
2 days ago
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
2 days ago
6
If you choose a book for dummies, what do you expect?
– molbdnilo
2 days ago
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
2 days ago
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
2 days ago
|
show 1 more comment
4
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
2 days ago
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
2 days ago
6
If you choose a book for dummies, what do you expect?
– molbdnilo
2 days ago
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
2 days ago
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
2 days ago
4
4
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
2 days ago
Yes you declare those variables, and define them. What you don't do is initialize them.
– Some programmer dude
2 days ago
4
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
2 days ago
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
2 days ago
6
6
If you choose a book for dummies, what do you expect?
– molbdnilo
2 days ago
If you choose a book for dummies, what do you expect?
– molbdnilo
2 days ago
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
2 days ago
@molbdnilo, a clear and easy-to-understand explanation that's factually correct, though. :) Writing 'for dummies' books does not justify factual mistakes.
– John Allison
2 days ago
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
2 days ago
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
2 days ago
|
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
2 days ago
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
2 days ago
Not sure which expression you refer to.
– Maxim Egorushkin
2 days ago
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
2 days ago
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
2 days ago
|
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
2 days ago
@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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
|
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
2 days ago
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
2 days ago
Not sure which expression you refer to.
– Maxim Egorushkin
2 days ago
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
2 days ago
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
2 days ago
|
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
2 days ago
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
2 days ago
Not sure which expression you refer to.
– Maxim Egorushkin
2 days ago
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
2 days ago
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
2 days ago
|
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 2 days ago
answered 2 days ago
songyuanyao
88.4k11170232
88.4k11170232
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
2 days ago
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
2 days ago
Not sure which expression you refer to.
– Maxim Egorushkin
2 days ago
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
2 days ago
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
2 days ago
|
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
2 days ago
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
2 days ago
Not sure which expression you refer to.
– Maxim Egorushkin
2 days ago
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
2 days ago
@MaximEgorushkin That means the expression of cppreference.com is not accurate either..?
– songyuanyao
2 days ago
Not sure which expression you refer to.
– Maxim Egorushkin
2 days ago
Not sure which expression you refer to.
– Maxim Egorushkin
2 days ago
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
2 days ago
@MaximEgorushkin the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values.
– songyuanyao
2 days ago
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
2 days ago
@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
2 days ago
|
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
2 days ago
@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
2 days ago
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
2 days ago
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
2 days ago
@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
2 days ago
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
2 days ago
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 2 days ago
answered 2 days ago
gsamaras
47.9k2394174
47.9k2394174
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
2 days ago
@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
2 days ago
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
2 days ago
add a comment |
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
2 days ago
@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
2 days ago
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
2 days ago
1
1
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
2 days ago
stackoverflow.com/questions/1410563/… defines
– Fantastic Mr Fox
2 days ago
@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
2 days ago
@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
2 days ago
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
2 days ago
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
2 days ago
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 2 days ago
answered 2 days ago
Maxim Egorushkin
83.3k1198179
83.3k1198179
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
|
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
|
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 2 days ago
answered 2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
|
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
2
2
A variable declaration only must include keyword
extern
, otherwise it is a definition. The initializer is a separate concern.– Maxim Egorushkin
2 days ago
A variable declaration only must include keyword
extern
, otherwise it is a definition. The initializer is a separate concern.– Maxim Egorushkin
2 days ago
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
2 days ago
@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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
@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
2 days ago
|
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
2 days ago
4
You are correct, and yes, unfortunately there are a lot of crappy books out there.
– 500 - Internal Server Error
2 days ago
6
If you choose a book for dummies, what do you expect?
– molbdnilo
2 days ago
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
2 days ago
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
2 days ago