best way of initialize an array?

A

aaragon

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,

 
V

Victor Bazarov

aaragon said:
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
 
C

Chris ( Val )

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
 
C

Chris ( Val )

[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.
 
A

aaragon

[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.

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?
 
C

Chris ( Val )

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

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.

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
 
V

Victor Bazarov

Chris said:
[..]
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top