best way of initialize an array?

Discussion in 'C++' started by aaragon, Sep 28, 2007.

  1. aaragon

    aaragon Guest

    Hi, just a very simple question. I was wondering what is the most
    efficient way of initializing an array. I have a very simple class
    that wraps an array to provide bound checking, something like this:

    enum checkMode {CHECK, NOCHECK};

    template <int n, checkMode c = CHECK>
    struct ArrayStructure {

    static const short dim_ = n;

    double& operator[](size_t i) {
    if(i >= dim_ || i < 0)
    throw RuntimeError("*** ERROR *** Array access out of
    bounds. \
    \nCheck the index when accesing the
    fitness or constraint array.");
    return storage;
    }
    double storage[n]; //!< storage for elements
    };

    Now, I need that the storage[n] be initialized to zeros. Is there a
    way to do this without having to write the constructor? If not, and I
    have to write it anyways, is there a way to initialize ALL values in
    the container in the initialization list? (I believe the
    initialization list is always the most efficient way to initialize
    variables, to avoid the assignment call). Thank you,

    aaragon, Sep 28, 2007
    #1
    1. Advertising

  2. aaragon wrote:
    > Hi, just a very simple question. I was wondering what is the most
    > efficient way of initializing an array. I have a very simple class
    > that wraps an array to provide bound checking, something like this:
    >
    > enum checkMode {CHECK, NOCHECK};
    >
    > template <int n, checkMode c = CHECK>
    > struct ArrayStructure {
    >
    > static const short dim_ = n;
    >
    > double& operator[](size_t i) {
    > if(i >= dim_ || i < 0)
    > throw RuntimeError("*** ERROR *** Array access out of
    > bounds. \
    > \nCheck the index when accesing the
    > fitness or constraint array.");
    > return storage;
    > }
    > double storage[n]; //!< storage for elements
    > };
    >
    > Now, I need that the storage[n] be initialized to zeros. Is there a
    > way to do this without having to write the constructor? If not, and I
    > have to write it anyways, is there a way to initialize ALL values in
    > the container in the initialization list? (I believe the
    > initialization list is always the most efficient way to initialize
    > variables, to avoid the assignment call). Thank you,


    Initialise the array just like you would any other member, with
    empty parentheses after the name. That is "value-initialisation"
    which for PODs (doubles included) results in zero-initialistaion.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 28, 2007
    #2
    1. Advertising

  3. On Sep 28, 3:24 pm, aaragon <> wrote:
    > Hi, just a very simple question. I was wondering what is the most
    > efficient way of initializing an array. I have a very simple class
    > that wraps an array to provide bound checking, something like this:
    >
    > enum checkMode {CHECK, NOCHECK};
    >
    > template <int n, checkMode c = CHECK>
    > struct ArrayStructure {
    >
    > static const short dim_ = n;
    >
    > double& operator[](size_t i) {
    > if(i >= dim_ || i < 0)
    > throw RuntimeError("*** ERROR *** Array access out of
    > bounds. \
    > \nCheck the index when accesing the
    > fitness or constraint array.");
    > return storage;
    > }
    > double storage[n]; //!< storage for elements
    >
    > };
    >
    > Now, I need that the storage[n] be initialized to zeros. Is there a
    > way to do this without having to write the constructor? If not, and I
    > have to write it anyways, is there a way to initialize ALL values in
    > the container in the initialization list? (I believe the
    > initialization list is always the most efficient way to initialize
    > variables, to avoid the assignment call). Thank you,


    Yes, an initialiser list is always the prefered place to initialise
    object, however, arrays are initialised a little differently.

    I'm nor sure why you are mixing short with int, but I would
    start with something simple like the following, and then
    build from there:

    template<int MaxSize> struct ArrayStructure {

    static double storage[ MaxSize ];

    double& operator[]( int Index )
    {
    if( Index > MaxSize || Index < std::size_t(0) )
    throw std::runtime_error("Array access out of bounds");

    return storage[ Index ];
    }
    };

    // statics are usually initialised outside the class
    // like this...
    template<int MaxSize>
    double ArrayStructure<MaxSize>::storage[ MaxSize ] = { 0 };

    int main()
    {
    ArrayStructure<5> MyArray;

    for( std::size_t Index( 0 ); Index < 5; ++Index )
    std::cout << MyArray[ Index ] << std::endl;

    std::cin.get();
    return 0;
    }

    HTH,
    Chris Val
    Chris ( Val ), Sep 28, 2007
    #3
  4. On Sep 28, 10:29 pm, "Chris ( Val )" <> wrote:
    > On Sep 28, 3:24 pm, aaragon <> wrote:


    [snip]

    > if( Index > MaxSize || Index < std::size_t(0) )
    > throw std::runtime_error("Array access out of bounds");


    [snip]

    Ignore that std::size_t, and replace with int for now.

    I added a std::size_t there to remove incompatible type
    warnings and so it would match your size_t parameter, but
    I forgot to remove it when I decided it was simpler for
    demonstration purposes to just use int.

    --
    Chris Val
    Chris ( Val ), Sep 28, 2007
    #4
  5. aaragon

    aaragon Guest

    On Sep 28, 7:34 am, "Chris ( Val )" <> wrote:
    > On Sep 28, 10:29 pm, "Chris ( Val )" <> wrote:
    >
    > > On Sep 28, 3:24 pm, aaragon <> wrote:

    >
    > [snip]
    >
    > > if( Index > MaxSize || Index < std::size_t(0) )
    > > throw std::runtime_error("Array access out of bounds");

    >
    > [snip]
    >
    > Ignore that std::size_t, and replace with int for now.
    >
    > I added a std::size_t there to remove incompatible type
    > warnings and so it would match your size_t parameter, but
    > I forgot to remove it when I decided it was simpler for
    > demonstration purposes to just use int.
    >
    > --
    > Chris Val


    Well, but I cannot use static member variable for the actual array,
    can I? If I use that, the class will have only one instantiation of
    that array for all the instantiated objects, and I need many many
    copies of this class. Am I wrong here?
    aaragon, Sep 28, 2007
    #5
  6. On Sep 28, 11:43 pm, aaragon <> wrote:
    > On Sep 28, 7:34 am, "Chris ( Val )" <> wrote:
    >
    >
    >
    >
    >
    > > On Sep 28, 10:29 pm, "Chris ( Val )" <> wrote:

    >
    > > > On Sep 28, 3:24 pm, aaragon <> wrote:

    >
    > > [snip]

    >
    > > > if( Index > MaxSize || Index < std::size_t(0) )
    > > > throw std::runtime_error("Array access out of bounds");

    >
    > > [snip]

    >
    > > Ignore that std::size_t, and replace with int for now.

    >
    > > I added a std::size_t there to remove incompatible type
    > > warnings and so it would match your size_t parameter, but
    > > I forgot to remove it when I decided it was simpler for
    > > demonstration purposes to just use int.

    >
    > > --
    > > Chris Val

    >
    > Well, but I cannot use static member variable for the actual array,
    > can I? If I use that, the class will have only one instantiation of
    > that array for all the instantiated objects, and I need many many
    > copies of this class. Am I wrong here?- Hide quoted text -


    Sorry, I somehow got it in my head that you wanted a static
    data member.

    Short of using a loop in the constructor, you can use
    std::fill() to set each element to zero:

    ArrayStructure() {
    std::fill( storage, storage + MaxSize, 0 );
    }

    Cheers,
    Chris Val
    Chris ( Val ), Sep 28, 2007
    #6
  7. Chris ( Val ) wrote:
    > [..]
    > Short of using a loop in the constructor, you can use
    > std::fill() to set each element to zero:
    >
    > ArrayStructure() {
    > std::fill( storage, storage + MaxSize, 0 );
    > }


    ArrayStructure() : storage() {}

    Acheives it better.

    V

    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 28, 2007
    #7
  8. On Sep 29, 12:27 am, "Victor Bazarov" <> wrote:
    > Chris ( Val ) wrote:
    >
    > > [..]
    > > Short of using a loop in the constructor, you can use
    > > std::fill() to set each element to zero:

    >
    > > ArrayStructure() {
    > > std::fill( storage, storage + MaxSize, 0 );
    > > }

    >
    > ArrayStructure() : storage() {}


    Yes, that is a much better solution.

    Thanks Victor,
    Chris Val
    Chris ( Val ), Sep 28, 2007
    #8
  9. aaragon

    aaragon Guest

    On Sep 28, 9:42 am, "Chris ( Val )" <> wrote:
    > On Sep 29, 12:27 am, "Victor Bazarov" <> wrote:
    >
    > > Chris ( Val ) wrote:

    >
    > > > [..]
    > > > Short of using a loop in the constructor, you can use
    > > > std::fill() to set each element to zero:

    >
    > > > ArrayStructure() {
    > > > std::fill( storage, storage + MaxSize, 0 );
    > > > }

    >
    > > ArrayStructure() : storage() {}

    >
    > Yes, that is a much better solution.
    >
    > Thanks Victor,
    > Chris Val


    Thank you all...
    aaragon, Oct 2, 2007
    #9
    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. cppaddict
    Replies:
    16
    Views:
    19,730
    m_pahlevanzadeh
    Jul 11, 2012
  2. David T. Ashley
    Replies:
    6
    Views:
    518
    Tim Rentsch
    Dec 5, 2004
  3. Replies:
    1
    Views:
    349
    Chris Brat
    Sep 17, 2006
  4. David T. Ashley
    Replies:
    6
    Views:
    368
    Tim Rentsch
    Dec 5, 2004
  5. slack_justyb
    Replies:
    2
    Views:
    452
    Gabriel
    Sep 1, 2005
Loading...

Share This Page