Re: We do not use C++ exceptions at Google
On 19 Jan, 20:19, Marsh Ray <marsh...@gmail.com> wrote:
On Jan 18, 4:50 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Jan 15, 6:08 pm, Marsh Ray <marsh...@gmail.com> wrote:
[...]
std::swap<int>(a, b); // Probably pretty darn efficient.
It as efficient as swap_c_style. See below.
swap_c_style(&a, &b);
The implementation of swap_c_style has to either check both args for
null, or produce undefined behavior over its domain of input values.
No checks necessary, it should just dump a core file for your
examination.
Maybe there is a time and place to just go ahead and dereference that
pointer, I mean, what would you do anyway, die with "internal error
1623847" message? So maybe your compiler and target guarantee a good
dump file. And there's a good way to get that dump back to the
developers. And your customers are cool with that (no sensitive data
in it, they have connectivity, don't mind your app crashing and
filling up the disk with core files, etc).
So if the stars align right, you get a nice crash dump landing back in
your (boss's) lap (with your code right at the top of the stack).
Still, doesn't seem like a great way to get a reputation as a
developer of quality code.
If you're not so lucky that day, well:
http://www.google.com/search?q=null+pointer+exploit
Or you could just use references.
You are trying to solve a good problem (NULL-pointer dereference),
however, you are hacking on the leaves, rather than on the root of the
it.
The root of the problem is that pointers, being what they are, require
good skill and care. The only real solution is discipline.
The compiler probably has a harder job optimizing away the case where
(*a == *b), too.
Why would it?
Because:
1. The domain of a pointer type includes this value called 'null',
whereas references do not.
Where does it help?
It only helps with casts over a hierarchy, because a pointer cast that
adds a positive or negative delta to the address must not adjust NULL
pointer. Whereas doing the same cast using a reference destination
type does not have to check its input for NULL.
2. Pointer types allow arithmetic, whereas references do not.
These possibilities require a higher level of sophistication on the
part of the compiler to make this particular optimization.
Sometimes it won't even be possible. Like if swap_c_style is called
from code that's compiled (and even linked) separately?
A reference argument to a function is the same as a
pointer argument on the binary level.
On some, maybe even most, compilers. But not necessarily in general.
References were introduced to the language to allow for operator
overloading. It's essentially a constant pointer with automatic
address-of operator application to the initialiser and automatic
dereference. There are also "reference to const can be bound to an r-
value", "reference is assumed to be non-NULL", and "no pointer
arithmetic" relaxations on top of the standard pointer semantics. But
that is about it.
In every other sense a reference is just a mere pointer.
Let's compare [...]
Can you spot the difference?
Sure, some compilers will generate identical code for some examples.
Which compilers will not?
--
Max
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]