Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #81582 > unrolled thread
| Started by | "das...@gmail.com" <dashley@gmail.com> |
|---|---|
| First post | 2021-09-25 11:03 -0700 |
| Last post | 2021-09-26 23:05 -0700 |
| Articles | 7 on this page of 27 — 13 participants |
Back to article view | Back to comp.lang.c++
NULL versus 0 "das...@gmail.com" <dashley@gmail.com> - 2021-09-25 11:03 -0700
Re: NULL versus 0 Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-25 18:16 +0000
Re: NULL versus 0 Paavo Helde <myfirstname@osa.pri.ee> - 2021-09-25 21:20 +0300
Re: NULL versus 0 Paavo Helde <myfirstname@osa.pri.ee> - 2021-09-25 21:32 +0300
Re: NULL versus 0 "das...@gmail.com" <dashley@gmail.com> - 2021-09-25 12:08 -0700
Re: NULL versus 0 Juha Nieminen <nospam@thanks.invalid> - 2021-09-27 05:47 +0000
Re: NULL versus 0 Bo Persson <bo@bo-persson.se> - 2021-09-27 08:32 +0200
Re: NULL versus 0 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-09-26 23:37 -0700
Re: NULL versus 0 "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-09-27 10:32 +0200
Re: NULL versus 0 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-27 11:32 -0700
Re: NULL versus 0 James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-09-27 15:03 -0400
Re: NULL versus 0 "see.my....@gmail.com" <see.my.homepage@gmail.com> - 2021-09-25 13:05 -0700
Re: NULL versus 0 Jorgen Grahn <grahn+nntp@snipabacken.se> - 2021-09-26 06:58 +0000
Re: NULL versus 0 Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-26 07:47 +0000
Re: NULL versus 0 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-26 02:15 -0700
Re: NULL versus 0 Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-26 09:38 +0000
Re: NULL versus 0 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-26 15:03 -0700
Re: NULL versus 0 Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-29 02:29 +0000
Re: NULL versus 0 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-28 20:34 -0700
Re: NULL versus 0 David Brown <david.brown@hesbynett.no> - 2021-09-29 08:52 +0200
Re: NULL versus 0 Bo Persson <bo@bo-persson.se> - 2021-09-26 13:54 +0200
Re: NULL versus 0 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-26 15:17 -0700
Re: NULL versus 0 Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-09-29 02:26 +0000
Re: NULL versus 0 Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-26 02:17 -0700
Re: NULL versus 0 HorseyWorsey@the_stables.com - 2021-09-26 14:20 +0000
Re: NULL versus 0 James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-09-25 17:36 -0400
Re: NULL versus 0 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-09-26 23:05 -0700
Page 2 of 2 — ← Prev page 1 [2]
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Date | 2021-09-26 13:54 +0200 |
| Message-ID | <irb5brF7b6gU1@mid.individual.net> |
| In reply to | #81597 |
On 2021-09-26 at 09:47, Branimir Maksimovic wrote: > 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++. > > It is not a bug when it is done very much on purpose. 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."
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2021-09-26 15:17 -0700 |
| Message-ID | <87wnn3j828.fsf@nosuchdomain.example.com> |
| In reply to | #81601 |
Bo Persson <bo@bo-persson.se> writes:
> On 2021-09-26 at 09:47, Branimir Maksimovic wrote:
>> 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++.
>>
> It is not a bug when it is done very much on purpose.
>
> 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."
"The C++ Programming Language", 4th edition, section 7.2.1.
As Stroustrup says, converting from void* to another pointer type
requires an explicit conversion, but converting NULL to any pointer
type does not. (void*)0 is a null pointer constant in C, but not in C++.
This:
int *p;
p = (void*)0;
is invalid in C++, but this:
int *p;
p = NULL;
is valid. Because of that, a C++ implementation that defines NULL as
either (void*)0 or ((void*)0) is non-conforming. (It's unlikely that
any actual C++ implementations make that mistake.)
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | Branimir Maksimovic <branimir.maksimovic@gmail.com> |
|---|---|
| Date | 2021-09-29 02:26 +0000 |
| Message-ID | <R9Q4J.17928$d82.10493@fx21.iad> |
| In reply to | #81601 |
On 2021-09-26, Bo Persson <bo@bo-persson.se> wrote: > On 2021-09-26 at 09:47, Branimir Maksimovic wrote: >> 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++. >> >> > It is not a bug when it is done very much on purpose. > > 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." > > Problem is that you can convert to unknown type, but compiler doesn't just converto to enyother type. This is simply pointless as compiler cannot possibly know what is unknown type, cast or not :P Let's take *any* implicit conversion then, like new languages DO :P -- 7-77-777 Evil Sinner!
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Date | 2021-09-26 02:17 -0700 |
| Message-ID | <875yunlmqs.fsf@nosuchdomain.example.com> |
| In reply to | #81596 |
Jorgen Grahn <grahn+nntp@snipabacken.se> writes:
> 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.
As I recall, Stroustrup's books used 0 when a null pointer constant was
required, and that was probably the origin of the (pre-nullptr)
convention to use 0 in C++ while NULL is more widely recommended in C.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
[toc] | [prev] | [next] | [standalone]
| From | HorseyWorsey@the_stables.com |
|---|---|
| Date | 2021-09-26 14:20 +0000 |
| Message-ID | <sipvjh$b7r$1@gioia.aioe.org> |
| In reply to | #81596 |
On 26 Sep 2021 06:58:15 GMT 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. It still use NULL instead of nullptr because its a lot more obvious when speed reading code.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Date | 2021-09-25 17:36 -0400 |
| Message-ID | <sio4os$ut0$1@dont-email.me> |
| In reply to | #81582 |
On 9/25/21 2:03 PM, das...@gmail.com wrote: > 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? In C, NULL can be defined as (void*)0, which is safer than 0, because it cannot be accidentally used in arithmetic contexts, which is a good reason to prefer NULL. In C++, NULL is not permitted to expand to (void*)0, but it can be defined as nullptr, which is not only safer than 0, but also is safer than (void*)0, so NULL should be preferred in C++, too. However, as a general rule explicit use of nullptr would be even better, because NULL could simply be defined as 0.
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2021-09-26 23:05 -0700 |
| Message-ID | <sirmuu$ofp$1@gioia.aioe.org> |
| In reply to | #81582 |
On 9/25/2021 11:03 AM, das...@gmail.com wrote:
> 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.
>
void* foo = 0;
void* foobar = NULL;
void* foobarCpp = nullptr;
Means foo == foobar == foobarCpp.
So, they should all be the same. Well, an impl can define these things
to mean a "null" pointer on their system, so to speak. Magic! nullptr
might mean something odd, and exotic under the hood... So does 0 wrt
pointers... ;^)
_____________________________
#include <iostream>
int main()
{
void* foo = 0;
void* foobar = NULL;
void* foobarCpp = nullptr;
std::cout << "foo = " << foo << "\n";
std::cout << "foobar = " << foobar << "\n";
std::cout << "foobarCpp = " << foobarCpp << "\n";
return 0;
}
_____________________________
Well, shit... Whats your output?
Can you even compile the damn thing?
;^)
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c++
csiph-web