a little confussion about initialization

M

Magcialking

here an example from <<c++primer(3rd edition)>>:
template<class elemtype>
void Array<elemtype>::grow(){

elemtype *oldia=_ia;
int oldsize=_size;
_size=size+size/2+1;
_ia=new elemtype[_size];

int ix;

for(ix=0;ix<oldsize;ix++)
_ia[ix]=oldia[ix];
///////////////////////
for(;ix<size;ix++)
_ia[ix]=elemtype();
//////////////////////
delete [] oldia;
}

what's the code between "/////" for?
 
R

Ron Natalie

Magcialking said:
here an example from <<c++primer(3rd edition)>>:
template<class elemtype>
void Array<elemtype>::grow(){

elemtype *oldia=_ia;
int oldsize=_size;
_size=size+size/2+1;
_ia=new elemtype[_size];

int ix;

for(ix=0;ix<oldsize;ix++)
_ia[ix]=oldia[ix];
///////////////////////
for(;ix<size;ix++)
_ia[ix]=elemtype();
//////////////////////
delete [] oldia;
}

what's the code between "/////" for?
It fills the entries in the "grown" part
(the ones after oldsize up to newsize)
by assigning a default constructed elemtype
to it.

Of course if elemtype is not POD, then it
already has been default initialized, and
the assignment is spurious. A better
solution is used by most vector classes
where it uses copy-construction on the
uninitialized storage with placement new.
 
A

Andrey Tarasevich

Ron said:
Magcialking said:
here an example from <<c++primer(3rd edition)>>:
template<class elemtype>
void Array<elemtype>::grow(){

elemtype *oldia=_ia;
int oldsize=_size;
_size=size+size/2+1;
_ia=new elemtype[_size];

int ix;

for(ix=0;ix<oldsize;ix++)
_ia[ix]=oldia[ix];
///////////////////////
for(;ix<size;ix++)
_ia[ix]=elemtype();
//////////////////////
delete [] oldia;
}

what's the code between "/////" for?
It fills the entries in the "grown" part
(the ones after oldsize up to newsize)
by assigning a default constructed elemtype
to it.

In the post-TC1 C++ that would be a "value-initialized elemtype", which
in general case is not the same as "default-initialized elemtype".
Of course if elemtype is not POD, then it
already has been default initialized, and
the assignment is spurious.

It is probably worth noting that in the post-TC1 C++ the second
initialization performs a value-initialization while the first one
(implicitly present in 'new[]') performs a default initialization. Which
means that it might actually do something useful also for non-POD class
types lacking a user-defined default constructor.
 
R

Ron Natalie

Andrey Tarasevich wrote:

Of course if elemtype is not POD, then it
already has been default initialized, and
the assignment is spurious.

It is probably worth noting that in the post-TC1 C++ the second
initialization performs a value-initialization while the first one
(implicitly present in 'new[]') performs a default initialization. Which
means that it might actually do something useful also for non-POD class
types lacking a user-defined default constructor.

Yes, of course, I forgot the god-awful bandaid language
terminology change installed to get around the utter stupidity
of C++ not always using defualt initialization when appropriate.
 
F

Frederick Gotham

Magcialking posted:
here an example from <<c++primer(3rd edition)>>:


Spelling, grammar and punctuation.

template<class elemtype>
void Array<elemtype>::grow(){

elemtype *oldia=_ia;
int oldsize=_size;
_size=size+size/2+1;
_ia=new elemtype[_size];

int ix;

for(ix=0;ix<oldsize;ix++)
_ia[ix]=oldia[ix];
///////////////////////
for(;ix<size;ix++)
_ia[ix]=elemtype();
//////////////////////
delete [] oldia;
}


This is extremely poor code -- do not try to learn from it.

Presuming that "size" is a typo for "_size", a slight improvement would be:

template<class ElemT>
void Array<ElemT>::Grow()
{
ElemT const *const p_old = _ia;

size_t const old_size = _size;

_size = _size + _size/2 + 1;

_ia = new ElemT[_size]();

{
ElemT *pdest = _ia;
ElemT const *const pdover = _ia + old_size;
ElemT const *psrc = p_old;

do
{
*pdest++ = *psrc++;
}while(pdest != pdover);
}

delete [] p_old;
}

If you want more efficiency, you might consider using "placement new" in
conjuction with "aligned_storage".
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top