I'm reading "Sams Teach Yourself C++ in One Hour a Day (6th Edition)".
(I'm already an expert-level C programmer, learning C++. I just say that as notice that you don't need to overexplain.)
In the examples in the book, they assign pointers to 0, typically at definition, i.e.
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more traditional to use NULL. I believe the value assigned is going to be the same, whether 0 or NULL is used.
Is one style preferred over the other in C++? Why?
25.09.2021 21:03 das...@gmail.com kirjutas:
I'm reading "Sams Teach Yourself C++ in One Hour a Day (6th Edition)".
(I'm already an expert-level C programmer, learning C++. I just say
that as notice that you don't need to overexplain.)
In the examples in the book, they assign pointers to 0,
Oh, and if this book predates C++11 (i.e. 2011), be aware that some
content is out-dated and it does not cover the myriad of new
enhancements introduced by C++11. You might want to use a newer book.
C++11 is quite essential for using C++ effectively nowadays. Later
additions have been smaller and more specialized, so can be postponed
for a while.
I'm reading "Sams Teach Yourself C++ in One Hour a Day (6th Edition)".
(I'm already an expert-level C programmer, learning C++. I just say that as notice that you don't need to overexplain.)
In the examples in the book, they assign pointers to 0, typically at definition, i.e.
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more traditional to use NULL. I believe the value assigned is going to be the same, whether 0 or NULL is used.
Is one style preferred over the other in C++? Why?
Thanks.
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more traditional to use NULL.
Is one style preferred over the other in C++? Why?
I'm reading "Sams Teach Yourself C++ in One Hour a Day (6th Edition)".
(I'm already an expert-level C programmer, learning C++. I just say that as notice that you don't need to overexplain.)
In the examples in the book, they assign pointers to 0, typically at definition, i.e.
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more traditional to use NULL. I believe the value assigned is going to be the same, whether 0 or NULL is used.
Is one style preferred over the other in C++? Why?
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more
traditional to use NULL.
It would be more traditional in C++, as well.
On Sat, 2021-09-25, see.my....@gmail.com wrote:
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more
traditional to use NULL.
It would be more traditional in C++, as well.
Opinions varied. Before nullptr, I always used NULL in C and 0 in
C++. IIRC using 0 was a common recommendation here, too, for whatever reason.
On 2021-09-26, Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:
On Sat, 2021-09-25, see.my....@gmail.com wrote:
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more
traditional to use NULL.
It would be more traditional in C++, as well.
Opinions varied. Before nullptr, I always used NULL in C and 0 in
C++. IIRC using 0 was a common recommendation here, too, for whatever
reason.
#define NULL (void*)0
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
#define NULL (void*)0
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
That's not a valid definition of NULL in either C or C++.
In C or C++, the definition has to be parenthesized to avoid parsing
errors in some contexts;
#define NULL ((void*0)
I'm not sure what you mean by "this is BuG in C++". A C++
implementation that defined NULL that way would be non-conforming.
On Sat, 2021-09-25, see.my....@gmail.com wrote:
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more
traditional to use NULL.
It would be more traditional in C++, as well.
Opinions varied. Before nullptr, I always used NULL in C and 0 in
C++. IIRC using 0 was a common recommendation here, too, for whatever reason.
On 2021-09-26, Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:
On Sat, 2021-09-25, see.my....@gmail.com wrote:
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more
traditional to use NULL.
It would be more traditional in C++, as well.
Opinions varied. Before nullptr, I always used NULL in C and 0 in
C++. IIRC using 0 was a common recommendation here, too, for whatever
reason.
#define NULL (void*)0
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
On Sat, 2021-09-25, see.my....@gmail.com wrote:
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more
traditional to use NULL.
It would be more traditional in C++, as well.
Opinions varied. Before nullptr, I always used NULL in C and 0 in
C++. IIRC using 0 was a common recommendation here, too, for whatever >reason.
On 2021-09-26, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Exactly, that is why it is Bug.#define NULL (void*)0
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
That's not a valid definition of NULL in either C or C++.
In C or C++, the definition has to be parenthesized to avoid parsing
errors in some contexts;
#define NULL ((void*0)
I'm not sure what you mean by "this is BuG in C++". A C++
implementation that defined NULL that way would be non-conforming.
Thanks for correction, BTW.
On 2021-09-26 at 09:47, Branimir Maksimovic wrote:
On 2021-09-26, Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:It is not a bug when it is done very much on purpose.
On Sat, 2021-09-25, see.my....@gmail.com wrote:#define NULL (void*)0
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more
traditional to use NULL.
It would be more traditional in C++, as well.
Opinions varied. Before nullptr, I always used NULL in C and 0 in
C++. IIRC using 0 was a common recommendation here, too, for whatever
reason.
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
Stroustrup says:
"A pointer to any type of object can be assigned to a variable of type
void*, a void* can be assigned to another void*, void* can be compared
for equality and inequality, and a void* can be explicitly converted
to another type. Other operations would be unsafe because the compiler
cannot know what kind of object is really pointed to. Consequently,
other operations result in compile-time errors."
I'm reading "Sams Teach Yourself C++ in One Hour a Day (6th Edition)".
(I'm already an expert-level C programmer, learning C++. I just say that as notice that you don't need to overexplain.)
In the examples in the book, they assign pointers to 0, typically at definition, i.e.
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more traditional to use NULL. I believe the value assigned is going to be the same, whether 0 or NULL is used.
Is one style preferred over the other in C++? Why?
Thanks.
In C++ (unlike C), NULL is a macro defined to 0, so there is no
difference.
On 2021-09-27 at 07:47, Juha Nieminen wrote:
Paavo Helde <myfirstname@osa.pri.ee> wrote:
In C++ (unlike C), NULL is a macro defined to 0, so there is no
difference.
Actually, many standard library implementations define NULL to be
nullptr (if we are in C++11 or newer).
(I haven't checked the standard, but this tells me that the standard does
not mandate NULL to be defined as 0.)
It just says
"The macro NULL is an implementation-defined null pointer constant."
And then a footnote saying that 0 is one possibility, but (void*)0 is not.
Paavo Helde <myfirstname@osa.pri.ee> wrote:
In C++ (unlike C), NULL is a macro defined to 0, so there is no
difference.
Actually, many standard library implementations define NULL to be
nullptr (if we are in C++11 or newer).
(I haven't checked the standard, but this tells me that the standard does
not mandate NULL to be defined as 0.)
On 2021-09-27 at 07:47, Juha Nieminen wrote:
Paavo Helde <myfirstname@osa.pri.ee> wrote:
In C++ (unlike C), NULL is a macro defined to 0, so there is no
difference.
Actually, many standard library implementations define NULL to be
nullptr (if we are in C++11 or newer).
(I haven't checked the standard, but this tells me that the standard does
not mandate NULL to be defined as 0.)
It just says
"The macro NULL is an implementation-defined null pointer constant."
And then a footnote saying that 0 is one possibility, but (void*)0 is not.
Paavo Helde <myfirstname@osa.pri.ee> wrote:
In C++ (unlike C), NULL is a macro defined to 0, so there is no
difference.
Actually, many standard library implementations define NULL to be
nullptr (if we are in C++11 or newer).
(I haven't checked the standard, but this tells me that the standard does
not mandate NULL to be defined as 0.)
On 27 Sep 2021 08:32, Bo Persson wrote:
On 2021-09-27 at 07:47, Juha Nieminen wrote:
Paavo Helde <myfirstname@osa.pri.ee> wrote:It just says
In C++ (unlike C), NULL is a macro defined to 0, so there is no
difference.
Actually, many standard library implementations define NULL to be
nullptr (if we are in C++11 or newer).
(I haven't checked the standard, but this tells me that the standard does >>> not mandate NULL to be defined as 0.)
"The macro NULL is an implementation-defined null pointer constant."
And then a footnote saying that 0 is one possibility, but (void*)0
is not.
C++17 §7.11/1:
❝A /null pointer constant/ is an integer literal with value zero or a prvalue of type `std::nullptr_t`.❞
As I recall the insistence on a null pointer constant being a literal
was introduced in C++11; the C++03 definition was
C++03 §4.10/1:
❝A /null pointer constant/ is an integral constant expression rvalue
of integer type that evaluates to zero.❞
A subtle but perhaps important change.
On 2021-09-26 at 09:47, Branimir Maksimovic wrote:
On 2021-09-26, Jorgen Grahn <grahn+nntp@snipabacken.se> wrote:It is not a bug when it is done very much on purpose.
On Sat, 2021-09-25, see.my....@gmail.com wrote:
int *pInt = 0;
Nothing inherently wrong with that, but in C, it would be more
traditional to use NULL.
It would be more traditional in C++, as well.
Opinions varied. Before nullptr, I always used NULL in C and 0 in
C++. IIRC using 0 was a common recommendation here, too, for whatever
reason.
#define NULL (void*)0
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
Stroustrup says:
"A pointer to any type of object can be assigned to a variable of type
void*, a void* can be assigned to another void*, void* can be compared
for equality and inequality, and a void* can be explicitly converted to another type. Other operations would be unsafe because the compiler
cannot know what kind of object is really pointed to. Consequently,
other operations result in compile-time errors."
Branimir Maksimovic <branimir.maksimovic@gmail.com> writes:
On 2021-09-26, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Exactly, that is why it is Bug.#define NULL (void*)0
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
That's not a valid definition of NULL in either C or C++.
In C or C++, the definition has to be parenthesized to avoid parsing
errors in some contexts;
#define NULL ((void*0)
I'm not sure what you mean by "this is BuG in C++". A C++
implementation that defined NULL that way would be non-conforming.
Thanks for correction, BTW.
To be clear, you're saying that if any C++ implementation actually
defined NULL that way, it would be a bug in that implementation.
That's true, but I'm not aware of any implementation that actually
has that hypothetical bug. It can require some trivial extra work
for an implementation that shares headers between C and C++, but
implementers are well aware of the issue.
On 2021-09-26, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Branimir Maksimovic <branimir.maksimovic@gmail.com> writes:We have problem off overoading resolution then with constant zero.
On 2021-09-26, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Exactly, that is why it is Bug.#define NULL (void*)0
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
That's not a valid definition of NULL in either C or C++.
In C or C++, the definition has to be parenthesized to avoid parsing
errors in some contexts;
#define NULL ((void*0)
I'm not sure what you mean by "this is BuG in C++". A C++
implementation that defined NULL that way would be non-conforming.
Thanks for correction, BTW.
To be clear, you're saying that if any C++ implementation actually
defined NULL that way, it would be a bug in that implementation.
That's true, but I'm not aware of any implementation that actually
has that hypothetical bug. It can require some trivial extra work
for an implementation that shares headers between C and C++, but
implementers are well aware of the issue.
Still, despite nullptr. You can accidentally mean 0 the integer
and have overload with pointer argument, and hav COMPILER BUG.
This should be corrected in future standard...
Branimir Maksimovic <branimir.maksimovic@gmail.com> writes:
On 2021-09-26, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Branimir Maksimovic <branimir.maksimovic@gmail.com> writes:We have problem off overoading resolution then with constant zero.
On 2021-09-26, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Exactly, that is why it is Bug.#define NULL (void*)0
simple, problem is that in C++, one have to cast from void
and that does not works, and this is BuG in C++.
That's not a valid definition of NULL in either C or C++.
In C or C++, the definition has to be parenthesized to avoid parsing >>>>> errors in some contexts;
#define NULL ((void*0)
I'm not sure what you mean by "this is BuG in C++". A C++
implementation that defined NULL that way would be non-conforming.
Thanks for correction, BTW.
To be clear, you're saying that if any C++ implementation actually
defined NULL that way, it would be a bug in that implementation.
That's true, but I'm not aware of any implementation that actually
has that hypothetical bug. It can require some trivial extra work
for an implementation that shares headers between C and C++, but
implementers are well aware of the issue.
Still, despite nullptr. You can accidentally mean 0 the integer
and have overload with pointer argument, and hav COMPILER BUG.
This should be corrected in future standard...
I don't know what "COMPILER BUG" you're talking about.
Using 0 as a null pointer constant in the presence of overloaded
functions can lead to a *programming* bug. I'm not aware of any C++
compiler that has a bug in this area (i.e., handles it in a way that's inconsistent with what the language requires).
That kind of programming bug can be avoided by using nullptr rather than
0 (something that wasn't possible before C++11).
If you're suggesting changing the language so that 0 is no longer a null pointer constant, that would break tons of existing code.
Programming bugs, compiler bugs, and language bugs are three very
different things.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 113 |
Nodes: | 8 (0 / 8) |
Uptime: | 09:47:06 |
Calls: | 2,497 |
Calls today: | 14 |
Files: | 8,646 |
Messages: | 1,902,668 |