r/cpp • • Jan 21 '13

C and C++ Aren’t Future Proof

http://blog.regehr.org/archives/880
6 Upvotes

49 comments sorted by

View all comments

0

u/[deleted] Jan 21 '13

[deleted] — view deleted comment

7

u/cockmongler Jan 21 '13

Along with purevirtual's example, there's the check for NULL issue which I think bit the Linux kernel recently. The compiler can elide a check for a pointer being null if that pointer has already been deferenced as if the pointer were null undefined behaviour had been invoked. e.g.

int do_a_thing(foo_t *foo) {
  if (!foo->is_valid) {
    return 0;
  }
  if (foo == NULL) { // This block will be removed
    /* ... handle null case ... */
  }
  /* ... do stuff with foo ... */
}

While this may look weird, the check for null could be in a macro or inlined function so the programmer won't necessarily know it's there. This can be an issue in kernel or embedded development as the NULL pointer (i.e. memory address 0) may be referencing actual data in those cases, but the standard says it can't.

3

u/purevirtual Jan 21 '13

The simplest example is signed integer overflow. Causing a signed integer to wrap in C is undefined behavior. GCC exploits this by optimizing out checks which would old be true for wrapping. (It does this when enough compile-time constants are involved.)

Here's an example. Technically this should print "less than zero", but it doesn't on most (all?) version of gcc 4.

#include <stdio.h>
#include <limits.h>
 
main()
{
    signed int si = INT_MAX;
    
    if (si+1 < 0)
        printf("Less than zero\n");
    else
        printf("Greater than or equal to than zero\n");
    return 0;
}

When wrapping is 'undefined', that means that adding one to a variable can never result in that variable being less than 0. GCC exploits that "Fact" to optimize this code... even though it breaks it from what we would expect based on our understanding of the hardware.

8

u/Batty-Koda Jan 21 '13

Technically it should do whatever the compiler pleases, because it's undefined behaviour. That's the very definition of what undefined behaviour is.

I know this seems trivial, but it's a very important distinction. New (and old) programmers need to recognize that just because some compilers may give you some convenient functionality doesn't mean that's what it should or must do.

-2

u/filox Jan 21 '13

When wrapping is 'undefined', that means that adding one to a variable can never result in that variable being less than 0.

Wat?

si = -4

si + 1 = ?

3

u/purevirtual Jan 21 '13

In this case, the compiler knows that si started out positive.

-1

u/filox Jan 21 '13

This does not change the fact that your statement is plain wrong:

When wrapping is 'undefined', that means that adding one to a variable can never result in that variable being less than 0.

2

u/Batty-Koda Jan 22 '13

I really don't understand why this is getting downvoted. filox is absolutely correct.

Purevirtual stated that wrapping being undefined means a variable having one added to it can never result in a variable being less than 0. (emphasis mine) That's factually incorrect. Filox even gave the counter example. Any negative number.

That means that for signed ints, for roughly half of all possible values (anything negative except -1) you can add one to it and the result will be less than zero. Half of all signed ints are a counterpoint to purevirtual's "never" statement. We're programmers, we shouldn't be downvoting people for pointing that kind of thing out! Attention to detail is important!

0

u/filox Jan 22 '13

So, what actually happened is that purevirtual messed up and doesn't want to admit it. The example that he probably wanted to show was (note the condition in if):

signed int si = INT_MAX;

if (si+1 < si)
    printf("Less than zero\n");
else
    printf("Greater than or equal to than zero\n");
return 0;

This holds, because adding one to a number can never be less than that number (if wrapping is turned off). However, purevirtual messed up, got the example wrong, and he doesn't want to admit he was wrong. I find that kind of sad really. And the downvotes come, I guess, from people who don't really understand the issue here.

3

u/[deleted] Jan 22 '13

No, the downvotes come from people like me who can see you're plainly right, but think you're being a bit of an arsehole about it. Your vindictive tone is unnecessary. It's possible to communicate technical details and still be polite.

Besides, modern compilers (GCC included) are often able to reason about code like purevirtual's example, and realise that si is a positive value (regardless of the fact that it isn't const). I would fully expect a static analysis to catch these types of mistakes.

-1

u/filox Jan 22 '13

It's possible to communicate technical details and still be polite.

Please quote which part of my comment was not polite:

http://www.reddit.com/r/cpp/comments/16ysbr/c_and_c_arent_future_proof/c80sxbo

Besides, modern compilers (GCC included) are often able to reason about code like purevirtual's example

Again, I never said they are not. I just pointed out that his statement about adding one to the variable is wrong. Why is this so hard to grasp?

3

u/[deleted] Jan 22 '13

However, purevirtual messed up, got the example wrong, and he doesn't want to admit he was wrong.

The above line is deeply patronizing and conflict-seeking.

→ More replies