Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > init of class members : mem(0) vs. mem() vs. not-init at all

Reply
Thread Tools

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

 
 
news.aon.at
Guest
Posts: n/a
 
      01-26-2011
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.




--
mit freundlichen Grüßen

mario semo

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-26-2011
On 01/26/11 08:51 PM, news.aon.at wrote:
> Hello,
>
> what is the expected behaviour for
>
> mem()


0

> vs.
>
> mem(0)


0

> vs.
>
> no - init at all


No initialisation, random value.

> for a long class member?


<snip>

> 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.


Avoid old compilers!

--
Ian Collins
 
Reply With Quote
 
 
 
 
news.aon.at
Guest
Posts: n/a
 
      01-26-2011
Ian,

thanks for your answer. one more question:

"Ian Collins" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On 01/26/11 08:51 PM, news.aon.at wrote:
>> Hello,
>>
>> what is the expected behaviour for
>>
>> mem()

>
> 0
>


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

regards, mario

 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-27-2011
news.aon.at wrote:
> "Ian Collins" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
>> On 01/26/11 08:51 PM, news.aon.at wrote:
>>> Hello,
>>>
>>> what is the expected behaviour for
>>>
>>> mem()

>>
>> 0
>>

>
> 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

 
Reply With Quote
 
news.aon.at
Guest
Posts: n/a
 
      01-27-2011
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 200 initialize the variable.

mario.



 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-27-2011
On Jan 27, 6:49 am, Ulrich Eckhardt <(E-Mail Removed)> wrote:
> news.aon.at wrote:
> > "Ian Collins" <(E-Mail Removed)> schrieb im Newsbeitrag
> >news:(E-Mail Removed)...
> >> On 01/26/11 08:51 PM, news.aon.at wrote:


> >>> what is the expected behaviour for


> >>> mem()


> >> 0


> > 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.


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.

--
James Kanze
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      01-27-2011
news.aon.at Wrote:

>
> 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

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      01-27-2011
Ian Collins <(E-Mail Removed)> wrote:
>> no - init at all

>
> No initialisation, random value.


Would that be a good source of random numbers?-)
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      01-27-2011
"James Kanze" <(E-Mail Removed)>
>> >>> mem()

>
>> >> 0

>
>> > 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.

>
> 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.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-28-2011
On Jan 27, 11:16 pm, "Balog Pal" <(E-Mail Removed)> wrote:
> "James Kanze" <(E-Mail Removed)>
> >> >>> mem()


> >> >> 0


> >> > 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.


> > 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...


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).

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
questions about object initialization, default-init and value-init Jess C++ 4 05-04-2007 02:47 AM
class members vs instance members hdixon Python 3 07-09-2006 06:56 PM
Sequence Order between Page Init and User Control Init Tony Cheng ASP .Net 1 02-24-2006 01:56 PM
Compiler/Linker Error undefined reference to 'std::ios_base::Init::Init[in-charge]() clusardi2k@aol.com C++ 1 08-18-2005 07:11 PM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments