Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialization of array as a member using the initialization list

Reply
Thread Tools

initialization of array as a member using the initialization list

 
 
aaragon
Guest
Posts: n/a
 
      11-01-2008
Hello everyone,

Is this valid?

template <class A>
struct ClassA {

typedef A value_type;
value_type* data_;

explicit ClassA(size_t n) : data(new value_type[]()) {} // note
the () after []


// other stuff
};


Here A is any type, including primitive types. I run valgrind on this
code and it gives me the "Conditional jump or move depends on
uninitialised value(s)" error. So if it is not valid, why is it? Is
there a way to default initialize the array in the initialization
list?

Thank you all,

aa
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      11-02-2008
On Nov 1, 6:57*pm, aaragon <(E-Mail Removed)> wrote:
> Hello everyone,
>
> Is this valid?
>
> template <class A>
> struct ClassA {
>
> * * typedef A value_type;
> * * value_type* data_;


// if its a pointer, label it as such

value_type* p_data;

>
> * * explicit ClassA(size_t n) : data(new value_type[]()) {} * *// note
> the () after []
>


// all elements of an array are default constructed.
// in fact, if a default ctor is not available for
// that template parameter, you lose

explicit ClassA(const std::size_t n)
: p_data(new value_type[n]) { }

~ClassA() { delete [] p_data; }

> * * // other stuff
>
> };
>
> Here A is any type, including primitive types. I run valgrind on this
> code and it gives me the "Conditional jump or move depends on
> uninitialised value(s)" error. So if it is not valid, why is it? Is
> there a way to default initialize the array in the initialization
> list?
>
> Thank you all,
>
> aa


A much better solution is to use a std::vector<>, where elements are
stored in contiguous memory like an array. Another container that is a
workhoerse is std::deque<>, elements are not contiguous. Advantages
imclude that these containers are dynamic / resizeable, allocate and
deallocate automatically. Blends very nicely with common algorithms
(random access iterator, begin/end). It has a common interface. Very
powerfull and a LOT easier to use. Its elements need not have a
default ctor:

class E { explicit E(double n) { } };

std::vector< E > vd(1000, E(99.9));

On the other hand, if i were to reinvent the wheel, I'ld design a
fixed array with a const size_t as a template parameter.

template< class A, const std::size_t Size >
struct Array
{
typedef A value_type;
// ctors
Array() : data() {}
explicit Array(const A& a)
{
for(value_type* pv = &data[0]; pv != &data[Size]; ++pv)
{
*pv = a;
}
}
// and so on ...
private:
value_type data[Size];
};
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-02-2008
On Nov 2, 3:13*am, Salt_Peter <(E-Mail Removed)> wrote:
> On Nov 1, 6:57*pm, aaragon <(E-Mail Removed)> wrote:
> > template <class A>
> > struct ClassA {

>
> > * * typedef A value_type;
> > * * value_type* data_;


> // if its a pointer, label it as such


> * * * value_type* p_data;


That's very bad advice. It's already labeled as a pointer;
that's what the * means. If being a pointer is an essential
part of its semantics, which you do want to reflect in the name,
then something like dataPointer can be used, but in my
experience, this isn't needed that much.

(The one thing that is sometimes useful is a scope indicator;
something like myData or m_data to indicate that it is a member
variable. In theory, if your names are good, it shouldn't be
necessary either, but in practice, it's sometimes an easy way
out.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error C2614: 'CYYYRegister' : illegal member initialization:'CRequest' is not a base or member Angus C++ 1 03-06-2008 11:38 AM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments