Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > When a default constructor necessary?

Reply
Thread Tools

When a default constructor necessary?

 
 
Sharad Kala
Guest
Posts: n/a
 
      07-25-2004

"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * Sharad Kala:

[snip]
> It's a bit of work to do correctly. Modulo alignment issues and
> exception issues and whatnot (omitted here... ) it goes like


Quite good but not quite

> #include <new>
>
> class Point{ ... };
>
> int main()
> {
> size_t const nObjects = 54;
> char memory[nObjects*sizeof( Point )];
> Point* pObjects = 0;
>
> // Initialize array.
> char *pMem = memory;
> for( size_t i = 0; i < nObjects; ++i )
> {
> Point* pNew = ::new( pMem ) Point( i, -i ); // Whatever.
> if( pObjects == 0 ) { pObjects = pNew; }
> pMem += sizeof( Point );
> }
>
> // Use it.
> // Whatever, e.g.
> for( size_t i = 0; i < nObjects; ++i )
> {
> // std::cout << pObjects[i].x() << std::endl;
> }
>

The following is not the way arrays would destruct.

> // Destroy.
> Point* pObj = pObjects;
> for( size_t i = 0; i < nObjects; ++i, ++pObj )
> {
> pObject->~Point(); // Modulo syntax, check it out.
> }

Instead something like -
// Destroy.
Point* pObj = pObjects + sizeof(nObjects*sizeof( Point ));
for( size_t i = 0; i < nObjects; ++i, --pObj )
{
pObjects->~Point(); // Modulo syntax, check it out.
}
Right, Alf ?

-Sharad


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-25-2004
* Sharad Kala:
>
> "Alf P. Steinbach" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > * Sharad Kala:

> [snip]
> > It's a bit of work to do correctly. Modulo alignment issues and
> > exception issues and whatnot (omitted here... ) it goes like

>
> Quite good but not quite
>
> > #include <new>
> >
> > class Point{ ... };
> >
> > int main()
> > {
> > size_t const nObjects = 54;
> > char memory[nObjects*sizeof( Point )];
> > Point* pObjects = 0;
> >
> > // Initialize array.
> > char *pMem = memory;
> > for( size_t i = 0; i < nObjects; ++i )
> > {
> > Point* pNew = ::new( pMem ) Point( i, -i ); // Whatever.
> > if( pObjects == 0 ) { pObjects = pNew; }
> > pMem += sizeof( Point );
> > }
> >
> > // Use it.
> > // Whatever, e.g.
> > for( size_t i = 0; i < nObjects; ++i )
> > {
> > // std::cout << pObjects[i].x() << std::endl;
> > }
> >

> The following is not the way arrays would destruct.
>
> > // Destroy.
> > Point* pObj = pObjects;
> > for( size_t i = 0; i < nObjects; ++i, ++pObj )
> > {
> > pObject->~Point(); // Modulo syntax, check it out.
> > }

> Instead something like -
> // Destroy.
> Point* pObj = pObjects + sizeof(nObjects*sizeof( Point ));
> for( size_t i = 0; i < nObjects; ++i, --pObj )
> {
> pObjects->~Point(); // Modulo syntax, check it out.
> }
> Right, Alf ?


Yep, mea culpa (while in nitpicking mode: s/pObjects/pObj/).

Now there's only alignment (perhaps easier to fix the spec by
requiring dynamic allocation) and exceptions (thorny) and the
"whatnot" -- I'm not really sure -- to deal with...

std::vector has it much easier since for a compiler-specific
implementation it can rely on knowledge of, say, the internal
workings of operator delete[], and so avoid both the loop and
keeping track of the way the array was formed, but it's limited
in that it doesn't support per-object-specific arguments.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-25-2004
* Alf P. Steinbach:
> * Sharad Kala:
> > > }

> > Instead something like -
> > // Destroy.
> > Point* pObj = pObjects + sizeof(nObjects*sizeof( Point ));
> > for( size_t i = 0; i < nObjects; ++i, --pObj )
> > {
> > pObjects->~Point(); // Modulo syntax, check it out.
> > }
> > Right, Alf ?

>
> Yep, mea culpa (while in nitpicking mode: s/pObjects/pObj/).


And you also need to decrement pObj _before_ destructor call, and
to be really standard-compliant make sure it isn't decremented
last time out of the loop, and your address computation is very
wrong (I tend to assume such things are OK when scanning code
because if not then we usually know it), so it should look like

Point* pObj = pObjects + nObjects; // Past 1 is allowed.
for( size_t i = 0; i < nObjects; ++i )
{
--pObj;
pObj->~Point();
}

Hth.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      07-25-2004

"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * Alf P. Steinbach:
> > * Sharad Kala:
> > > > }
> > > Instead something like -
> > > // Destroy.
> > > Point* pObj = pObjects + sizeof(nObjects*sizeof( Point ));
> > > for( size_t i = 0; i < nObjects; ++i, --pObj )
> > > {
> > > pObjects->~Point(); // Modulo syntax, check it out.
> > > }
> > > Right, Alf ?

> >
> > Yep, mea culpa (while in nitpicking mode: s/pObjects/pObj/).

>
> And you also need to decrement pObj _before_ destructor call, and
> to be really standard-compliant make sure it isn't decremented
> last time out of the loop, and your address computation is very
> wrong (I tend to assume such things are OK when scanning code
> because if not then we usually know it), so it should look like


oh yeah, that address calculation was bogus

> Point* pObj = pObjects + nObjects; // Past 1 is allowed.
> for( size_t i = 0; i < nObjects; ++i )
> {
> --pObj;
> pObj->~Point();
> }


Still simpler -

Point* pObj = pObjects + nObjects; // Past 1 is allowed.
while( pObj != pObjects )
{
--pObj;
pObj->~Point();
}

-Sharad


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      07-25-2004
Alf P. Steinbach wrote in news:(E-Mail Removed) in
comp.lang.c++:

> Point* pObj = pObjects + nObjects; // Past 1 is allowed.
>



No the real array is char, so the above is "past sizeof( Point )"

.

nitpicking-ly yr's Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-25-2004
* Sharad Kala:
> Still simpler -
>
> Point* pObj = pObjects + nObjects; // Past 1 is allowed.
> while( pObj != pObjects )
> {
> --pObj;
> pObj->~Point();
> }


Well, that does one extra comparison compared to a do-while (assuming
nObjects > 0), yes? </g>

I like for-loops:

for( Point* p=pObjects+nObjects; (--p)->~Point(); p!=pObjects )
{}

(still modulo syntax)...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-25-2004
* Alf P. Steinbach:
> * Sharad Kala:
> > Still simpler -
> >
> > Point* pObj = pObjects + nObjects; // Past 1 is allowed.
> > while( pObj != pObjects )
> > {
> > --pObj;
> > pObj->~Point();
> > }

>
> Well, that does one extra comparison compared to a do-while (assuming
> nObjects > 0), yes? </g>
>
> I like for-loops:
>
> for( Point* p=pObjects+nObjects; (--p)->~Point(); p!=pObjects )
> {}
>
> (still modulo syntax)...


No fish biting here. Umph.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-25-2004
* Rob Williscroft:
> Alf P. Steinbach wrote in news:(E-Mail Removed) in
> comp.lang.c++:
>
> > Point* pObj = pObjects + nObjects; // Past 1 is allowed.
> >

>
>
> No the real array is char, so the above is "past sizeof( Point )"
>
> .
>


Heh. I don't know whether the Holy Standard covers this situation
or not. But practically, both "1 past end" pointers here refer to
the same address, because the character array is sized just so.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
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
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Mixing default and non-default parameters in Constructor? desktop C++ 2 06-06-2007 08:25 PM
Bug? Parametrized constructor invokes default constructor mario.rossi@REMOVETHISTOMAILMENOT.com C++ 4 10-12-2006 07:59 PM
How to call default constructor from other constructor of the same class? romayankin@gmail.com C++ 9 03-06-2006 09:30 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments