This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

empty slices: defined?


A few related questions:

If one explicitly constructs a slice or glice with the default
constructor, does the standard require this slice to have any usable
state?  It says "creates a slice which specifies no elements", which
could be interpreted two ways:

1. There are no elements to which the slice refers (i.e. undefined).
2. The slice specifies an array with no elements in it (i.e. defined).

While slice's default constructor's standard explicitly limits its use
to declaring arrays of slices, the gslice standard does not limit its
use.  How is the standard to be interpreted?  There is no obvious
"don't do that" nor is there an obvious "you can do that".

-- on to implementation questions --

However, if you attempt to do this, the program segfaults, because
libstdc++'s implementation dereferences a NULL pointer.  We carefully
initialize the pointer to NULL, and there are a number of places where
a NULL pointer is checked for, and if found, suitable "array of zero
elements" values are used, but not *all* places are protected - in
std_valarray.h there are two unprotected dereferences in the two
operator[](const gslice&) methods.  The first implies that the default
constructor means "set of zero", the second implies that it means
"undefined".  They don't segfault there because the code takes a
reference to them, but they do segfault later when you try to use the
array.

Also, what are the ramifications of having libstdc++ define the state
of a default-constructed slice as "array of zero elements"?  That
would make it slightly more useful and redirect any programmers of
invalid code to the other implementation's mailing lists ;-) (but at
the cost of performance)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]