[lug] new operator in C++
tkil at scrye.com
Sat Apr 27 14:57:08 MDT 2002
>>>>> "svq" == Stephen Queen <svq at peakpeak.com> writes:
svq> In using the new operator in C++ I cannot find whether it is
svq> guaranteed to initialize the new memory to zero or not. In
svq> playing with it, it looks like it does, but that might be
svq> coincidence. Does anyone know a source of information on the net
svq> regarding this?
i don't fully understand your question. the return value from new is,
by definition, a fully-constructed object. remember that C++ will
declare and define a default constructor if no other constructors are
given for a particular class.
if you asking whether the constructor sees initialized memory or not,
i think you can recurse the above definition. the only question then
becomes: what are the intrinsic types initialized to? the standard
does indeed indicate that they are zero-initialized by default. (at
least, that's how i read sec 8.5 para 5; it's a bit convoluted.) this
initialization is done before the body of the constructor is entered,
so the constructor code should never see non-initialized data.
did that answer your question?
svq> Another question regarding sources of information on C++. man has
svq> section 3 which is a good source of information for regular
svq> C. Does anyone know of anything like that for C++? It sure would
svq> come in handy now and then.
note that section 3 is really documentation for the libraries
installed on the system, and section 2 is for system calls. neither
one is really about programming C; it's assumed that you know how to
do that already, and it's just describing the interface.
there aren't many good free sources of similar information for C++.
one of the best is the C++ FAQ LITE:
there is probably some online documentation for the G++ libraries,
which are mostly standards-conforming. the SGI STL pages are
sometimes useful, although the STL is only a subset of the full C++
you can get an electronic copy of the C++ standard for only 18 USD
another useful source of free information on the net are the usenet
newsgroups. comp.lang.c++ is a good place to look.
if you are serious about programming C++, you should invest in some
good books. particularly:
Stroustrup, B. The C++ Programming Language (Special Ed.)
Josuttis, N. The C++ Standard Library
the Meyers books are also excellent (Effective C++ 2nd, More Effective
C++ [which might be due for another ed], Effective STL). the FAQ
points out that books can be divided into "legality" guides (which the
two above are good examples of), and "moralality" guides (Meyers):
finally, if you want to understand where C++ came from, and have most
of your "why does it do it this way?" or "why can't i do it that way?"
questions answered, Stroustrup's _Design & Evolution of C++_ is a
More information about the LUG