[lug] new operator in C++
tkil at scrye.com
Sat Apr 27 15:22:32 MDT 2002
>>>>> "Tkil" == tkil <tkil at scrye.com> writes:
Tkil> if you asking whether the constructor sees initialized memory or
Tkil> not, i think you can recurse the above definition. the only
Tkil> question then becomes: what are the intrinsic types initialized
Tkil> to? the standard does indeed indicate that they are
Tkil> zero-initialized by default. (at least, that's how i read sec
Tkil> 8.5 para 5; it's a bit convoluted.) this initialization is done
Tkil> before the body of the constructor is entered, so the
Tkil> constructor code should never see non-initialized data.
i retract this statement. in the case of using "new" on a plain old
data (POD) type, in a "new" expression with no initializers:
[expr.new] para 15:
A new-expression that creates an object of type T initializes that
object as follows:
- If the new-initializer is omitted:
- If T is a (possibly cv-qualified) non-POD class type (or array
thereof), the object is default-initialized (8.5) If T is a
const-qualified type, the underlying class type shall have a
user-declared default constructor.
- Otherwise, the object created has indeterminate value. If T is
a const-qualified type, or a (possibly cv-qualified) POD class
type (or array thereof) containing (directly or indirectly) a
member of const-qualified type, the program is ill-formed;
the important bit is the "indeterminate value" clause.
i think i got it right this time. the bit i cited in the previous
message had to do with static initialization; both automatic and
dynamic initializations have the above rules (that is, "if there are
no initializers, then it's indeterminate").
note that the allocation function proper is explicitly described as
not guaranteeing any particular contents in the returned memory
region. it's in the interest of security of the operating system,
however, that pages allocated to a program's data area are changed
from "whatever was there before", as that could leak sensitive
information from one user to another...
p.s. i was possibly confusing myself by some recent java experiences;
java *does* guarantee the zero initializer even for automatic and
More information about the LUG