Thread: Default Initialization of Aggregates / Arrays

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Default Initialization of Aggregates / Arrays

    In VC2005 I have something like this:

    Code:
    struct t
    {
            int ia[100];
            struct s { int a, b, c; } type;
            t() : ia(), type() { }
    };
    And I get the warning

    Code:
    warning C4351: new behavior: elements of array 'CWindow::_ia' will be default initialized
    What is that new behavior thing? Should I not depend on this behavior? Is there a better way to default initialize members of the array? Also, am I guaranteed that all members of type will be default initialized?

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I think this means that all the elements will be set by default to equal zero. Other than that, I've never seen that warning before.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    That's standard behavior.

    Arrays follow the same rules as other variables concerning default initialization.

    However, to default initialize an array, give it one value. All other dimensions will be zero-initialized if it is an array of a built-in type.

    Code:
    int ia[100] = {0};  // initializes first element to 0, and zero-initializes all other elements
    EDIT: Actually just payed attention to the warning. You compiler is bypassing the standard behavior by default initializing the array. That is non portable.
    Last edited by Mario F.; 11-19-2006 at 04:48 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can't have compound initialization in an initializer list, though.

    I wasn't aware the syntax in the first post was even legal.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    Arrays follow the same rules as other variables concerning default initialization.
    
    However, to default initialize an array, give it one value. All other dimensions will be zero-initialized if it is an array of a built-in type.
    
    
    Code:
    int ia[100] = {0};  // initializes first element to 0, and zero-initializes all other elements
    EDIT: Actually just payed attention to the warning. You compiler is bypassing the standard behavior by default initializing the array. That is non portable.
    Yeah, as CornedBee said, I can't do such a thing in an initializer list and certainly not the body of a constructor. Any way to default initialize the array portably, or should I just memset or something? Will that syntax ever be adopted into the standard?

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Being a struct you can always zero-initialize it at instantiation:

    Code:
    t object={0};
    But that's not what you are asking. Other than memset of explicit assignment, I don't think you can.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Curiously enough it also zero-initialized the array on MinGW 3.4.5

    Code:
    class a {
        public:
            int arr[10];
            a():arr() {}
    };
    It compiles with no errors with -ansi and -pedantic on.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Stroustrup isn't totally clear on whether the initializer list can be empty, but with gcc this works for me:

    Code:
    int ia[100] = {};
    Is this portable?

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's irrelevant to the problem. But yes, it's portable.

    I also found the place where it kind of allows default-initializing arrays in class initializer lists.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 08:09 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  4. OpenGL drawing w/ Arrays or not?
    By tegwin in forum Game Programming
    Replies: 2
    Last Post: 01-14-2003, 03:31 PM
  5. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 04:08 PM