Re: multi dimensional arrays

Discussion in 'C++' started by Ron Natalie, Jul 9, 2003.

  1. Ron Natalie

    Ron Natalie Guest

    "muser" <> wrote in message news:...
    > When initialising an Multi dimensional array, can you leave out the
    > bracket and therefore the content of the array?
    > For instance char temp1[2][2] = { }; or char temp1[2][2], are either
    > of these valid.


    The former is not valid, you must provide at least 1 initializer.
    char temp1[2][2] = { 0 };
    The latter potentially doesn't perform any initialization (it's a major defect
    of the language).

    > As a p.s. is char temp1[12][104] = {rec.customer_code,
    > rec.customer_address etc }; a valid way of initialising an MDA? where
    > rec is the name of a structure.


    Can't say without knowning what rec.customer_code is. But I suspect it's not.
     
    Ron Natalie, Jul 9, 2003
    #1
    1. Advertising

  2. "Shane Beasley" <> wrote...
    > "Ron Natalie" <> wrote in message

    news:<3f0c5ff2$0$87859$>...
    >
    > > > When initialising an Multi dimensional array, can you leave out the
    > > > bracket and therefore the content of the array?
    > > > For instance char temp1[2][2] = { }; or char temp1[2][2], are either
    > > > of these valid.

    > >
    > > The former is not valid, you must provide at least 1 initializer.
    > > char temp1[2][2] = { 0 };

    >
    > My compiler doesn't seem to mind the former, and neither does Annex A
    > of the Standard, whose initializer-clause term (A.7) includes an empty
    > initializer.


    You're right, actually. The empty list is allowed for arrays
    with known bounds, default-initialisation is performed on all
    elements in such case (and if they are of POD type, they are
    zero-initialised, according to 8.5/5). The proof is in A.7 as
    you mentioned and in 8.5.1, paragraphs 2, 4, 8. My note (as
    well as Ron's) was not correct.

    Live and learn...

    V
     
    Victor Bazarov, Jul 10, 2003
    #2
    1. Advertising

  3. Ron Natalie

    Ron Natalie Guest

    "Victor Bazarov" <> wrote in message news:...

    > My note (as well as Ron's) was not correct.


    I concur. There are a couple of places an empty initializer list is not allowed.
    This isn't one of them.
     
    Ron Natalie, Jul 10, 2003
    #3
  4. "Ron Natalie" <> wrote in message news:<3f0dd934$0$87835$>...

    > > Seriously, it was an intentional design decision, and lots of people
    > > support it. I tend to agree that C++ should zero-initialize by
    > > default, but only if those people who really need those clock cycles
    > > could turn it off. At any rate, I wouldn't call it a major defect,
    > > considering that it only matters when your program is buggy (using an
    > > uninitialized variable is a bug in my book).

    >
    > My programs are NOT buggy,


    Didn't mean to insinuate that. Rather, I asked myself, "Why should C++
    default-initialize variables?" Since default initialization would
    flush out certain types of programming errors (mostly dereferencing of
    uninitialized pointers), it contributes to the overall quality of C++
    programs, and thus might be a Good Thing for C++ to do.

    > but it's a royal pain in the ass to have to write constructors that
    > initialze the POD members when if the default initializiation were
    > performed the compiler generated default constructor would have sufficed.


    I see your point, but I'm not sure I'm convinced. I've always
    considered it self-evident that, if I want it initialized, I
    initialize it. From the looks of things, I'm not alone. For instance,
    in Java, which does default-initialize class members, the compiler
    warns you about using uninitialized local variables rather than
    default-initializing them as well. (I wonder if it would do the same
    for class members, halting problem aside.) In JavaScript and Perl,
    variables are default-initialized with an "undefined" value, and
    attempting to use that value results in an error. The implication is
    that use of uninitialized variables is most often accidental, and thus
    allowing/encouraging use of an uninitialized variable is tantamount to
    allowing/encouraging accidents.

    The one obvious counterexample, of course, is the C++ static variable.
    Why the designers chose to default-initialize here is a mystery to me,
    but that they didn't do the same elsewhere is obvious, albeit more
    pragmatic than insightful: if you don't use it (e.g., if you intend to
    initialize later anyway), you shouldn't have to pay for it. Arguing
    against that, especially after 30+ years of precedent (since C does
    the same), will be difficult.

    FWIW, I might be interested in Java-like member initialization syntax
    like this:

    class Foo {
    // ...
    private:
    int myValue = 0; // if no other initializer is given, use 0
    struct { int x, y, z; } myStruct = {}; // extension of the above
    };

    You still have to do it yourself, but you don't have to flesh out a
    constructor for it.

    - Shane
     
    Shane Beasley, Jul 11, 2003
    #4
    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. Replies:
    2
    Views:
    8,674
    Jim Lewis
    Mar 21, 2006
  2. Alf P. Steinbach
    Replies:
    0
    Views:
    436
    Alf P. Steinbach
    Aug 18, 2003
  3. John Harrison
    Replies:
    4
    Views:
    6,927
    Default User
    Aug 19, 2003
  4. Icosahedron
    Replies:
    8
    Views:
    656
    Vivek
    Aug 21, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    203
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page