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

Discussion in 'C++' started by news.aon.at, Jan 26, 2011.

  1. news.aon.at

    news.aon.at Guest

    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
     
    news.aon.at, Jan 26, 2011
    #1
    1. Advertising

  2. news.aon.at

    Ian Collins Guest

    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
     
    Ian Collins, Jan 26, 2011
    #2
    1. Advertising

  3. news.aon.at

    news.aon.at Guest

    Ian,

    thanks for your answer. one more question:

    "Ian Collins" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    news.aon.at, Jan 26, 2011
    #3
  4. news.aon.at wrote:
    > "Ian Collins" <> schrieb im Newsbeitrag
    > news:...
    >> 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
     
    Ulrich Eckhardt, Jan 27, 2011
    #4
  5. news.aon.at

    news.aon.at Guest

    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.
     
    news.aon.at, Jan 27, 2011
    #5
  6. news.aon.at

    James Kanze Guest

    On Jan 27, 6:49 am, Ulrich Eckhardt <> wrote:
    > news.aon.at wrote:
    > > "Ian Collins" <> schrieb im Newsbeitrag
    > >news:...
    > >> 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
     
    James Kanze, Jan 27, 2011
    #6
  7. 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
     
    Bart van Ingen Schenau, Jan 27, 2011
    #7
  8. Ian Collins <> wrote:
    >> no - init at all

    >
    > No initialisation, random value.


    Would that be a good source of random numbers?-)
     
    Juha Nieminen, Jan 27, 2011
    #8
  9. news.aon.at

    Balog Pal Guest

    "James Kanze" <>
    >> >>> 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.
     
    Balog Pal, Jan 27, 2011
    #9
  10. news.aon.at

    James Kanze Guest

    On Jan 27, 11:16 pm, "Balog Pal" <> wrote:
    > "James Kanze" <>
    > >> >>> 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
     
    James Kanze, Jan 28, 2011
    #10
  11. On Jan 26, 7:58 am, Ian Collins <> wrote:
    > On 01/26/11 08:51 PM, news.aon.at wrote:


    does news.aon.at buy blank lines wholesale?


    > > Hello,

    >
    > > what is the expected behaviour for

    >
    > > mem()

    >
    > 0


    wouldn't that depend on what the default constructor did?


    > > 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
     
    Nick Keighley, Jan 29, 2011
    #11
  12. news.aon.at

    Ian Collins Guest

    On 01/30/11 02:05 AM, Nick Keighley wrote:
    > On Jan 26, 7:58 am, Ian Collins<> wrote:
    >> On 01/26/11 08:51 PM, news.aon.at wrote:

    >
    >>> Hello,

    >>
    >>> what is the expected behaviour for

    >>
    >>> mem()

    >>
    >> 0

    >
    > wouldn't that depend on what the default constructor did?


    The OP asked "for a long class member?"

    Trimming is good.

    --
    Ian Collins
     
    Ian Collins, Jan 29, 2011
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,518
    Tony Morris
    Oct 19, 2004
  2. CoolPint
    Replies:
    8
    Views:
    1,027
    Jeff Schwab
    Dec 14, 2003
  3. hdixon
    Replies:
    3
    Views:
    673
    hdixon
    Jul 9, 2006
  4. Replies:
    8
    Views:
    336
    jaysome
    Mar 1, 2007
  5. Replies:
    6
    Views:
    343
Loading...

Share This Page