Peculiar Initialization 
Author Message
 Peculiar Initialization

I'm in the process of preparing a textbook on C/C++
programming for an introductory MIS class and was writing
the chapter on structures. My specific topic was the
differences between using composition and pointers.

To demonstrate, I defined two structures:

car -- composed of engine, body, interior and epa_safety
structures

car_pointer -- composed of pointers to same structures

(Definitions are in attached car.h).

I used the following main function:

// start main file

#include <iostream>
using namespace std;

#include "car.h"

int main()
{
        struct engine e1={6,4.1};
        struct body b1={"Hunter Green",81,183};
        struct interior in1={false,false};
        struct epa_safety epa1={0.00};
        struct car_pointer mycarptr=
            {10210,&e1,&b1,&in1,&epa1};
        struct car mycar= {10210, //nSerialNo
          {6,4.1},// engine initializers
          {"Hunter Green",81,183}, // body initilizers
          {false,false} // interior initializers
        };
        cout << mycar.nSerialNo << endl;
        cout << mycarptr.nSerialNo << endl;
        return 0;

Quote:
}

// end main file

For some reason, however, the interior structure embedded
within the mycar object did not initialize the same way
the interior object in1 did. I would have expected the
non-initialed struct interior elements (i.e., bCDPlayer
and szColor[]) to have been initialized to the zero-
equivalents. Instead, running to the cout statements, I
found:

mycar.in.bCDPlayer == true
mycar.in.szColor == four garbage characters, followed by
NULs

On the other hand, I found:

in1.bCDPlayer == false
in1.szColor ==> all NUL'd out
   (Just as I would haver expected.)

Does anyone know of any obvious reasons that these
elements might not have initialized the same way, given
they had the same initialization list (i.e.,
{false,false})?

Thanks!

  car.h
< 1K Download


Sun, 09 Jan 2005 02:44:07 GMT  
 Peculiar Initialization

Quote:
> I'm in the process of preparing a textbook on C/C++
> programming for an introductory MIS class and was writing
> the chapter on structures. My specific topic was the
> differences between using composition and pointers.

That sounds like a compiler bug, IMO.

According to the C++ standard:

8.5.1/1-2 says:

An aggregate is an array or a class (clause 9) with no user-declared
constructors (12.1), no private or protected non-static data members (clause
11), no base classes (clause 10), and no virtual functions (10.3).

When an aggregate is initialized the initializer can be an
initializer-clause consisting of a brace-enclosed, comma-separated list of
initializers for the members of the aggregate, written in increasing
subscript or member order. If the aggregate contains subaggregates, this
rule applies recursively to the members of the subaggregate.

... and 8.5.1/7 says:

If there are fewer initializers in the list than there are members in the
aggregate, then each member not explicitly initialized shall be
default-initialized (8.5).

So there should be no difference between your two examples, unless I'm
overlooking something :)

-cd



Sun, 09 Jan 2005 04:17:44 GMT  
 Peculiar Initialization


Quote:


> > I'm in the process of preparing a textbook on C/C++
> > programming for an introductory MIS class and was writing
> > the chapter on structures. My specific topic was the
> > differences between using composition and pointers.

> That sounds like a compiler bug, IMO.

Yes, this is another conformance issue that is already fixed in our upcoming
release sometimes called VC7.1.

Jason Shirk
VC++ Compiler Team



Sun, 09 Jan 2005 09:41:01 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. very peculiar problem

2. fwrite- Peculiar behaviour

3. URGENT, reg:peculiar ARRAy usage

4. Peculiar reason for posting

5. qsort - peculiar behaviour (Borland C++ 3.1)

6. Peculiar errors in database (ASSERT) Please help

7. peculiar problem in VC++ app.

8. Peculiar strstr error

9. Peculiar problem in Array

10. Peculiar problem with MFC Regular DLLs

11. peculiar problem with popen/pclose (with lq-text)

12. Peculiar Mapping

 

 
Powered by phpBB® Forum Software