Re: Trouble finding information in C++ standard
Roy Smith wrote:
"James Kanze" <james.kanze@gmail.com> wrote:
If you use a variable, it must be initialized. And if you don't
use it, there's no need to even define it.
I came upon a counter-example to this just yesterday. I'm using the
getaddrinfo() call. It requires passing in one of these:
struct addrinfo {
int ai_flags; /* input flags */
int ai_family; /* protocol family for socket */
int ai_socktype; /* socket type */
int ai_protocol; /* protocol for socket */
socklen_t ai_addrlen; /* length of socket-address */
struct sockaddr *ai_addr; /* socket-address for socket */
char *ai_canonname; /* canonical name for service location */
struct addrinfo *ai_next; /* pointer to next in list */
};
The struct is used for both input and output. On input, only the first 4
elements are used (the others get filled in on output). So, initializing
the last 4 would be a waste of CPU cycles.
Have you measured it? Written code which fills in the last 4,
and then compared actual execution time with that of code which
didn't? (And of course, if the compiler did the initialization,
it could be even faster.)
Granted, one can argue that this is a stupid way to design an API (and I
would probably agree),
Yes and no. The API was designed to work with C. Neither C nor
C++ has out paramaters, so why not? It's not very elegant, but
it more or less works.
The absense of out parameters can mean extra CPU time.
Measurable time, in some cases. If I have a function which
constructs a complex data structure, for example, I have the
choice of either constructing the structure before calling the
function, at possibly extra cost compared to first constructing
it when the actual information is available (in the called
function), or copying it to return it as a value (and copying
could potentially be expensive).
but I gotta work with what I'm given. One could
also argue that the extra work of initializing an extra int and 3 pointers
is minimal (and I would probably agree with that too).
On could even argue that it isn't even measurable, especially if
the compiler does it. Do you know how long getaddrinfo() takes?
Yet, this is a
legitimate example of where you have to define something but it doesn't
have to be initialized.
There are lots of cases. Even the simplest:
int i ;
std::cin >> i ;
There's no *need* to initialize i. If initialization by the
compiler were required, depending on how intelligent the
compiler was, it might be able to skip initializing such
variables. (Generally, however, that would take a very, very
smart compiler.) It can certainly skip initialization in a lot
of cases---the cases which in Java correspond to "definite
assignment". And how often will the left-over cases make a
measurable difference. In both your example, and my example
above, we're talking about not initializing a few bytes (less
than a microsecond on a modern machine), before calling a
function whose execution time is measured in tens of
milliseconds, at least.
The one exception I can think of is something like:
void
f()
{
double a[ 1000000 ] ;
initA( a ) ;
// ...
}
where the function initA is in another translation unit, and
calculates initial values and writes them to every element. If
the calculation is relatively simple, initializing the array
before calling initA could have a significant run-time cost, and
if initA is in another translation unit, it would require
fairlyl sophisticated global analysis to detect that it wasn't
necessary---some compilers today are capable of this, but the
technique isn't widespread.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]