init of class members : mem(0) vs. mem() vs. not-init at all

N

news.aon.at

Hello,

what is the expected behaviour for

mem()

vs.

mem(0)

vs.

no - init at all

for a long class member?


sample:

#include <stdio.h>

class Foo
{
public:

Foo()
: m1()
, m2()
, m3()
{
printf("m1=%d,m2=%d,m3=%d,m4=%d,m5=%d\n",m1,m2,m3,m4,m5);
}

virtual ~Foo() {}

long m1;
long m2;
long m3;
long m4;
long m5;

};

int main(int argc,char *argv[])
{
Foo aFoo;
Foo * aFoo2 = new Foo();
return 0;
}

i have an old compiler which does not init any of the vars and a very new
compiler which inits m1 , m2 , m3 with 0 and not m4 and m5.
 
N

news.aon.at

Ian,

thanks for your answer. one more question:

Ian Collins said:

Has this changed since ARM ? (i tested 3 "older" compilers (before 2000) and
all 3 do not initialize mem with mem().

regards, mario
 
U

Ulrich Eckhardt

news.aon.at said:
Has this changed since ARM ? (i tested 3 "older" compilers (before 2000)
and all 3 do not initialize mem with mem().

For the record, the first C++ standard was published in 99, IIRC, so
chances are that compilers didn't catch up within a year. Concerning
whether that has changed, I don't know. BTW, out of curiosity, what were
those decade-old compilers you tested and why?

Cheers!

Uli
 
N

news.aon.at

Uli,
For the record, the first C++ standard was published in 99, IIRC, so
chances are that compilers didn't catch up within a year. Concerning
whether that has changed, I don't know. BTW, out of curiosity, what were
those decade-old compilers you tested and why?

We are working in the area of development tools for automation industry,
power plants and building automation. They all use very long time living
products and so our products have long life times too.
I found the problem as i ported a product from IBMs Visual Age 3.5 for WinNT
(1995) to Microsoft Visual C++ 12.x (VS 2010).
(The variable initialized with mem() in VACPP is uninitialized - a bug in
the code - but init to 0 in VC++).

IBM stopped with C++ compilers for Windows after 3.5. Due to different
reasons we never ported our C++ code from IBMs to Microsoft Compilers till
2010. We still use VACPP for compiling our main product lines.

Note : my test program shows that MS VC++ 6.0 does not initialize the
variable too, while VC++ 9.0 (visual Studio 2008) initialize the variable.

mario.
 
J

James Kanze

For the record, the first C++ standard was published in 99,
IIRC, so chances are that compilers didn't catch up within
a year.

The first C++ standard was officialized in 1998 (not 1999).
Most, if not all, compiler vendors were members of the
committee, however, and once the final committee draft was
adopted (about a year before officialization), they could start
work. As this is a simple change, and has no effect on existing
code, I would expect it to be implemented fairly quickly, and
implementation to start as soon as it was adopted by the
committee. (On the other hand, IIRC, it was adopted rather late
in the standardization process.)
Concerning whether that has changed, I don't know.

It's definitely a change between the ARM and C++98.
BTW, out of curiosity, what were those decade-old compilers
you tested and why?

Note that such older compilers will likely differ from the
standard in many ways: perhaps no support for exceptions or
RTTI, shaky and non-standards compliant support for templates,
etc.
 
B

Bart van Ingen Schenau

news.aon.at said:
Has this changed since ARM ? (i tested 3 "older" compilers (before 2000)
and

all 3 do not initialize mem with
mem().

Yes, this has changed since ARM.
In the ARM days, T() was defined to give unspecified results for the
primitive types.
In the C++ standard, T() is defined to value-initialise the object, which
means setting it to 0 for primitive types.

Bart v Ingen Schenau
 
B

Balog Pal

James Kanze said:
The first C++ standard was officialized in 1998 (not 1999).
Most, if not all, compiler vendors were members of the
committee, however, and once the final committee draft was
adopted (about a year before officialization), they could start
work.

And they did too -- so crap like the CD2 version of auto_ptr made it to the
real life librarier and stuck there...

But value-init (I guess) we're talking about entered the standard only in
2003. So we can't expect compilers ~2000 having it.
 
J

James Kanze

And they did too -- so crap like the CD2 version of auto_ptr
made it to the real life librarier and stuck there...

Some things turned out to be more volatile than others:).
But value-init (I guess) we're talking about entered the
standard only in 2003. So we can't expect compilers ~2000
having it.

The exact specification of how initialization occurs was changed
in 2003, but the basic idea, that T() resulted in the same
initialization as when you declare a static T object, was
present in the 1998 version of the standard (although I think it
was proposed and added fairly late in the process).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top