473,320 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

NULL used in arithmetic

I got a warning from gcc for the following code

if (nextIs_.size() == NULL)

warning: NULL used in arithmetic

how can I fix this problem? It seems that I can install a patch for
gcc, but I don't have permission to do that, I can only change my code.

Thanks all.

Jul 1 '06 #1
20 41699
asdf posted:
I got a warning from gcc for the following code

if (nextIs_.size() == NULL)

NULL is intended to be used exclusively as a null pointer constant.

You're using it as a simple integer zero.

how can I fix this problem? It seems that I can install a patch for
gcc, but I don't have permission to do that, I can only change my code.

if ( !nextIs_.size() )
Or even:
if ( 0 == nextIs_.size() )

--

Frederick Gotham
Jul 1 '06 #2
asdf wrote:
I got a warning from gcc for the following code

if (nextIs_.size() == NULL)

warning: NULL used in arithmetic

how can I fix this problem? It seems that I can install a patch for
gcc, but I don't have permission to do that, I can only change my code.


NULL is a macro evaluating to a legal null pointer constant. A null
pointer constant is an integral expression with value zero. While
tecnhically, you can use it as an integer zero for other purposes,
the general intent is to use NULL when you are testing for null pointer
and 0 otherwise.

Further, if nextIs_ is some standard container, size() == 0 is an
potentially inefficient idiom. empty() evaluates in constant time
where as size() may take longer (it may actually have to count the
objects).
Jul 1 '06 #3
asdf wrote:
I got a warning from gcc for the following code

if (nextIs_.size() == NULL)

warning: NULL used in arithmetic

how can I fix this problem? It seems that I can install a patch for
gcc, but I don't have permission to do that, I can only change my code.


Gcc uses a non-Standard extension that makes NULL a keyword. It behaves like
a proper NULL, which is just 0, but it emits warnings when used as an
integer.

Get around it by not abusing NULL. You should never write it where you
actually mean an integer 0, such as to compare with the return value of
..size().

Replace the NULL with a zero!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 1 '06 #4
asdf wrote:
I got a warning from gcc for the following code

if (nextIs_.size() == NULL)

warning: NULL used in arithmetic

how can I fix this problem?
Well, stop using NULL in arithmetic. The above comparison should normally be
expressed as

if (nextIs_.size() == 0)

What on earth made you to use NULL there in the first place?

--
Best regards,
Andrey Tarasevich
Jul 2 '06 #5
Phlip wrote:
asdf wrote:
>>I got a warning from gcc for the following code

if (nextIs_.size() == NULL)

warning: NULL used in arithmetic

how can I fix this problem? It seems that I can install a patch for
gcc, but I don't have permission to do that, I can only change my code.

Gcc uses a non-Standard extension that makes NULL a keyword.
NULL is still a macro, as the standard requires, but it resolves to __null,
which is a compiler-specific keyword. This isn't required by the standard,
but it doesn't violate it either.

Jul 2 '06 #6
Andrey Tarasevich posted:
The above comparison should normally be expressed as

if (nextIs_.size() == 0)

I find that to be redundant, and instead write:
if ( !nextIs_.size() )
But nonetheless, A LOT of people seem to like the comparison to zero.

--

Frederick Gotham
Jul 2 '06 #7
Frederick Gotham wrote:
if ( !nextIs_.size() )

But nonetheless, A LOT of people seem to like the comparison to zero.
if (0 == nextIs_.size())

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 2 '06 #8
Frederick Gotham wrote:
Andrey Tarasevich posted:
>The above comparison should normally be expressed as

if (nextIs_.size() == 0)


I find that to be redundant, and instead write:
if ( !nextIs_.size() )
But nonetheless, A LOT of people seem to like the comparison to zero.
A lot of people (me included) believe that "comparison-less" form should only be
used with values that have pronounced boolean semantics. With non-boolean values
the explicit comparison operator is always required, even if it is a comparison
with 0.

And many of those who still use the "short" form with non-boolean values still
consider the above use of '!' to be completely unacceptable, since it doesn't
convey the actual meaning of the comparison very well. (Another popular example
of such terrible ridiculous use of '!' is its use with 'strcmp' in cases when
one watches for _equality_ of the strings).

--
Best regards,
Andrey Tarasevich
Jul 2 '06 #9
if (0 == nextIs_.size())

Note that .empty() is of course more generically efficient.

What I mean is that the expected value of a comparison should be on the
left, for various trivial reasons.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 2 '06 #10
Andrey Tarasevich posted:

A lot of people (me included) believe that "comparison-less" form
should only be used with values that have pronounced boolean
semantics. With non-boolean values the explicit comparison operator is
always required, even if it is a comparison with 0.

Incorrect.
void DoSomething(void) {}
double Func(void)
{
return 56.34;
}
int main()
{
if ( !Func() ) DoSomething();
}
(Another popular example of such terrible
ridiculous use of '!' is its use with 'strcmp' in cases when one
watches for _equality_ of the strings).

Different strokes for different folks.

I myself would write:

if ( !strcmp( /* args */ ) ) DoSomething();
--

Frederick Gotham
Jul 2 '06 #11
Frederick Gotham wrote:
Andrey Tarasevich posted:
>A lot of people (me included) believe that "comparison-less" form
should only be used with values that have pronounced boolean
semantics. With non-boolean values the explicit comparison operator is
always required, even if it is a comparison with 0.
Incorrect.
double Func(void)
if ( !Func() ) DoSomething();
I see no "secretly compare doubles to zero" in Andrey's paragraph. It seems
to me that "comparison with" can involve, say, 0.0 double.
Different strokes for different folks.

I myself would write:

if ( !strcmp( /* args */ ) ) DoSomething();
One good style rule is to pronounce code out loud and hear if it makes
sense. So "if not string compare" is bad style, not just "different
strokes".

if (0 == strcmp(...))

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 2 '06 #12
Rolf Magnus wrote:
Phlip wrote:
>asdf wrote:
>>I got a warning from gcc for the following code

if (nextIs_.size() == NULL)

warning: NULL used in arithmetic

how can I fix this problem? It seems that I can install a patch for
gcc, but I don't have permission to do that, I can only change my code.
Gcc uses a non-Standard extension that makes NULL a keyword.

NULL is still a macro, as the standard requires, but it resolves to __null,
which is a compiler-specific keyword. This isn't required by the standard,
but it doesn't violate it either.
As far as I know, the only value of null that is specifically banned
(18.1/4 footnote 180) is ((void*)0)
Jul 2 '06 #13
Frederick Gotham wrote:
>A lot of people (me included) believe that "comparison-less" form
should only be used with values that have pronounced boolean
semantics. With non-boolean values the explicit comparison operator is
always required, even if it is a comparison with 0.


Incorrect.
Huh? What's "incorrect"? And what is the purpose of that code sample?

I'm not claiming that this is prohibited by the language. I'm just saying that
by many people the comparison-less condition with non-boolean types is
considered _bad_ _coding_ _practice_, even though it is allowed by the language.
>
>(Another popular example of such terrible
ridiculous use of '!' is its use with 'strcmp' in cases when one
watches for _equality_ of the strings).


Different strokes for different folks.

I myself would write:

if ( !strcmp( /* args */ ) ) DoSomething();
...
And I would consider this completely unacceptable.

--
Best regards,
Andrey Tarasevich
Jul 3 '06 #14
Andrey Tarasevich posted:
Frederick Gotham wrote:
>>A lot of people (me included) believe that "comparison-less" form
should only be used with values that have pronounced boolean
semantics. With non-boolean values the explicit comparison operator
is always required, even if it is a comparison with 0.


Incorrect.

Huh? What's "incorrect"? And what is the purpose of that code sample?

You stated:

"With non-boolean values the explicit comparison operator is always
required, even if it is a comparison with 0."

Choosing a "double" as a non-boolean value, I demonstrated that the
explicit comparison operator was not required.

I'm not claiming that this is prohibited by the language. I'm just
saying that by many people the comparison-less condition with
non-boolean types is considered _bad_ _coding_ _practice_, even though
it is allowed by the language.

Labeling something as "bad coding practise" doesn't bear much gravity in
C++ -- consider the amount of expert programmers who still use "i++"
when the simpler alternative of "++i" would be viable -- Bjarne
Stroustrup himself does it.

>Different strokes for different folks.

I myself would write:

if ( !strcmp( /* args */ ) ) DoSomething();
...

And I would consider this completely unacceptable.

There are more ways to program in C++ than there are ways to skin a cat!
Consider:

if(!p) Vs if(p == NULL) Vs if(NULL==p) Vs if(p == 0) Vs if(0==p)
for(int i=0;i<len;i++) Vs for(unsigned i = 0; i != len; ++i)
You have your own way of doing things, and I have my own way of doing
things. Until the day that you're my boss, I'll program the way I like
to.
--

Frederick Gotham
Jul 3 '06 #15
Frederick Gotham wrote:
>>>A lot of people (me included) believe that "comparison-less" form
should only be used with values that have pronounced boolean
semantics. With non-boolean values the explicit comparison operator
is always required, even if it is a comparison with 0.

Incorrect.

Huh? What's "incorrect"? And what is the purpose of that code sample?

You stated:

"With non-boolean values the explicit comparison operator is always
required, even if it is a comparison with 0."

Choosing a "double" as a non-boolean value, I demonstrated that the
explicit comparison operator was not required.
You took that "required" out of context. By "required" I meant that it is
"always required" in certain coding standards/practices (either "official" or
self-imposed).
>I'm not claiming that this is prohibited by the language. I'm just
saying that by many people the comparison-less condition with
non-boolean types is considered _bad_ _coding_ _practice_, even though
it is allowed by the language.


Labeling something as "bad coding practise" doesn't bear much gravity in
C++ -- consider the amount of expert programmers who still use "i++"
when the simpler alternative of "++i" would be viable -- Bjarne
Stroustrup himself does it.
Firstly, I haven't seen too many "expert programmers" (whatever your criteria of
"expert programmer" is) who would do that. The same applies to the above
comparison practices.

Secondly, the argument based on "the amount of [expert] programmers" doesn't
bear much gravity either.
You have your own way of doing things, and I have my own way of doing
things. Until the day that you're my boss, I'll program the way I like
to.
Yes, and? That's, BTW, what I was saying from the very beginning. As for the
"boss" part, I don't understand how this has anything to do with what I said so far.

--
Best regards,
Andrey Tarasevich
Jul 3 '06 #16
red floyd wrote:
Rolf Magnus wrote:
>Phlip wrote:
>>asdf wrote:

I got a warning from gcc for the following code

if (nextIs_.size() == NULL)

warning: NULL used in arithmetic

how can I fix this problem? It seems that I can install a patch for
gcc, but I don't have permission to do that, I can only change my code.
Gcc uses a non-Standard extension that makes NULL a keyword.

NULL is still a macro, as the standard requires, but it resolves to
__null, which is a compiler-specific keyword. This isn't required by the
standard, but it doesn't violate it either.

As far as I know, the only value of null that is specifically banned
(18.1/4 footnote 180) is ((void*)0)
NULL must be a macro that resolves to a zero value of any integral type. The
g++ built-in __null is such an integral zero value.

Jul 3 '06 #17
Andrey Tarasevich wrote:
Frederick Gotham wrote:
>Andrey Tarasevich posted:
>>The above comparison should normally be expressed as

if (nextIs_.size() == 0)


I find that to be redundant, and instead write:
if ( !nextIs_.size() )
But nonetheless, A LOT of people seem to like the comparison to zero.

A lot of people (me included) believe that "comparison-less" form should
only be used with values that have pronounced boolean semantics. With
non-boolean values the explicit comparison operator is always required,
even if it is a comparison with 0.

And many of those who still use the "short" form with non-boolean values
still consider the above use of '!' to be completely unacceptable, since
it doesn't convey the actual meaning of the comparison very well.
I go with that. In the above case, I would also explicitly write down the
comparison with 0. However, when checking for null pointers, I think the
short form expresses the meaning quite well, like:

MyClass* p = somefunc();
if (!p)
{
// error
}

The !p here means to me "p does not point to an object", which expresses the
intent more clearly than p == NULL ("p is equal to a null pointer value").
(Another popular example of such terrible ridiculous use of '!' is its use
with 'strcmp' in cases when one watches for _equality_ of the strings).
How is the long form better here?

Jul 3 '06 #18
Andrey Tarasevich posted:

>Choosing a "double" as a non-boolean value, I demonstrated that the
explicit comparison operator was not required.

You took that "required" out of context. By "required" I meant that it
is "always required" in certain coding standards/practices (either
"official" or self-imposed).

Required by coding standards/practices.

Not required by the language.

Understood.
>Labeling something as "bad coding practise" doesn't bear much gravity
in C++ -- consider the amount of expert programmers who still use
"i++" when the simpler alternative of "++i" would be viable -- Bjarne
Stroustrup himself does it.

Firstly, I haven't seen too many "expert programmers" (whatever your
criteria of "expert programmer" is) who would do that. The same
applies to the above comparison practices.

The C++ Programming Language 3rd Edition by Bjarne Stroustrup contains
many instances of "i++".
>You have your own way of doing things, and I have my own way of doing
things. Until the day that you're my boss, I'll program the way I
like to.

Yes, and? That's, BTW, what I was saying from the very beginning. As
for the "boss" part, I don't understand how this has anything to do
with what I said so far.

What I was getting it is that you were decreeing my code to be
"unacceptable", and my response is that I'll program the way I like to
program, unless of course an employer explicitly instructs me to do
otherwise.

Thankfully though, I have no intention of every working as a computer
programmer.
--

Frederick Gotham
Jul 3 '06 #19
Frederick Gotham wrote:
The C++ Programming Language 3rd Edition by Bjarne Stroustrup contains
many instances of "i++".
If Bjarne just ... jumped off a cliff, would you?
Thankfully though, I have no intention of every working as a computer
programmer.
Apparently not. ;-)

Please don't debate style with those of us who must support colleagues,
okay?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 3 '06 #20
Phlip posted:
Frederick Gotham wrote:
>The C++ Programming Language 3rd Edition by Bjarne Stroustrup contains
many instances of "i++".

If Bjarne just ... jumped off a cliff, would you?

I use Bjarne as an example of an expert programmer, nothing more.

>Thankfully though, I have no intention of every working as a computer
programmer.

Apparently not. ;-)

Please don't debate style with those of us who must support colleagues,
okay?

Forgive, I'll keep my eliteness to myself.

--

Frederick Gotham
Jul 3 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
2
by: Stuart | last post by:
Hi there I have a stored procedure on my SQL database that retrieves a wide range of values from about 5 different tables. My end point is to calculate the cost against each line of retrieved...
4
by: Familjen Karlsson | last post by:
How will the code under look like in Visual Basic .Net, since you can't use the keyword null anymoore in Visual Basic .Net. What can I use instead. SqlConnection connection = null; SqlCommand...
5
by: Angel Tsankov | last post by:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
37
by: red floyd | last post by:
I searched the FAQ on this one, couldn't really find an answer. Stylistically, is it better to use 0 or NULL? I know that pre-Standard, 0 was recommended, because some compilers implemented...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
20
by: Quantum | last post by:
Hi, Are these equivalent: char text; if(text==NULL){} if(text==0){} Thank you,
12
by: semut | last post by:
Given that the string is of null terminated type. What could be the possible causes (by experience) the string to have no null terminated and cause buffer overflow later. I know it is quite broad,...
15
by: info | last post by:
Hi, I need this behaviour: 1 + null = 1 I have a (dynamic) set of many columns containing decimals that I want to add as follows: if all columns are null the result should be null if not all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.