PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Placement new for Arrays : How does VC++ handle it ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Placement new for Arrays : How does VC++ handle it ?

Réponse
 
LinkBack Outils de la discussion
Vieux 26/12/2007, 16h06   #1
sparc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Placement new for Arrays : How does VC++ handle it ?

Hi Guys,

I am encountering the following issue :

void* pMemory = m_Allocator.allocate( uiSize * sizeof( T ) );
m_atData = new( pMemory ) T[m_uiSize];

Debug.vc8.scurc.exe!`eh vector constructor iterator'(void *
ptr=0x00514b9c, unsigned int size=12, int count=12, void (void *)*
pCtor=0x00414537, void (void *)* pDtor=0x00414523) + 0x5e bytes C++

Do I need to code my own construct_n( ) ? I am ok with it but I just
want to know why there is an offset between m_atData and pMemory.

Technically I believe the m_atData should point to the same location
as pMemory after placement new is done. However this is turning out to
be false. The address of m_atData is addressof pMemory + 4. Iam
puzzled as this does not happen all the time. For some classes it
works fine and for others it dont.

I might be naive on this although I have tried debugging most of the
trivial stuff. Could someone please ?

Thanks in advance
Venkatesh.SC
  Réponse avec citation
Vieux 27/12/2007, 01h29   #2
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Placement new for Arrays : How does VC++ handle it ?

On Dec 26, 11:06 am, sparc <sc.venkat...@gmail.com> wrote:
> Hi Guys,
>
> I am encountering the following issue :
>
> void* pMemory = m_Allocator.allocate( uiSize * sizeof( T ) );
> m_atData = new( pMemory ) T[m_uiSize];
>
> Debug.vc8.scurc.exe!`eh vector constructor iterator'(void *
> ptr=0x00514b9c, unsigned int size=12, int count=12, void (void *)*
> pCtor=0x00414537, void (void *)* pDtor=0x00414523) + 0x5e bytes C++
>
> Do I need to code my own construct_n( ) ? I am ok with it but I just
> want to know why there is an offset between m_atData and pMemory.
>
> Technically I believe the m_atData should point to the same location
> as pMemory after placement new is done. However this is turning out to
> be false. The address of m_atData is addressof pMemory + 4. Iam
> puzzled as this does not happen all the time. For some classes it
> works fine and for others it dont.
>
> I might be naive on this although I have tried debugging most of the
> trivial stuff. Could someone please ?
>
> Thanks in advance
> Venkatesh.SC


read:
[11.10] What is "placement new" and why would I use it?
http://www.parashift.com/c++-faq-lit...html#faq-11.10

and pay carefull attention to the sections labelled ADVICE and DANGER
(its your responsability to insure that alignment of allocation area
and the object type concur)

Consider showing a compileable code snippet to illustrate the problem.
You don't need to post 1000 lines, a minimal test case will do.
  Réponse avec citation
Vieux 27/12/2007, 17h34   #3
sparc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Placement new for Arrays : How does VC++ handle it ?

Hi,


Thanks for the quick reply. I believe it probably is an issue with
the alignment although I haven't really digged deeper. I wrote my own
vector construtor iterator which works well anyway (which would work
in most platforms).

Thanks again,
Venkatesh.SC
  Réponse avec citation
Vieux 28/12/2007, 00h35   #4
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Placement new for Arrays : How does VC++ handle it ?

sparc wrote:
> Hi,
>
>
> Thanks for the quick reply. I believe it probably is an issue with
> the alignment although I haven't really digged deeper. I wrote my own
> vector construtor iterator which works well anyway (which would work
> in most platforms).
>
> Thanks again,
> Venkatesh.SC


The size that new[] needs from the allocator is M + n*sizeof(T)
where T is the type being allocated, n is the number of elements
in the array and M is some unspecified overhead value (typically
it's sizeof(size_t) or somethine like that).

  Réponse avec citation
Vieux 29/12/2007, 20h06   #5
sparc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Placement new for Arrays : How does VC++ handle it ?

On Dec 28, 8:35am, Ron Natalie <r...@spamcop.net> wrote:
> sparc wrote:
> > Hi,

>
> > Thanks for the quick reply. I believe it probably is an issue with
> > the alignment although I haven't really digged deeper. I wrote my own
> > vector construtor iterator which works well anyway (which would work
> > in most platforms).

>
> > Thanks again,
> > Venkatesh.SC

>
> The size that new[] needs from the allocator is M + n*sizeof(T)
> where T is the type being allocated, n is the number of elements
> in the array and M is some unspecified overhead value (typically
> it's sizeof(size_t) or somethine like that).


Hi,

Thanks. Yes. That is right. But I am just wondering why it adds for
certain
classes. I dont think a constructor iterator would need this overhead
due to inheritance as it does occur even for base types.

Btw, any idea why this overhead is added? Just curious. Thanks again
for the reply.

Venkatesh.SC
  Réponse avec citation
Vieux 29/12/2007, 20h39   #6
Dave Rahardja
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Placement new for Arrays : How does VC++ handle it ?

On 2007-12-29 14:06:59 -0600, sparc <sc.venkatesh@gmail.com> said:

> On Dec 28, 8:35am, Ron Natalie <r...@spamcop.net> wrote:
>> sparc wrote:
>>> Hi,

>>
>>> Thanks for the quick reply. I believe it probably is an issue with
>>> the alignment although I haven't really digged deeper. I wrote my own
>>> vector construtor iterator which works well anyway (which would work
>>> in most platforms).

>>
>>> Thanks again,
>>> Venkatesh.SC

>>
>> The size that new[] needs from the allocator is M + n*sizeof(T)
>> where T is the type being allocated, n is the number of elements
>> in the array and M is some unspecified overhead value (typically
>> it's sizeof(size_t) or somethine like that).

>
> Hi,
>
> Thanks. Yes. That is right. But I am just wondering why it adds for
> certain
> classes. I dont think a constructor iterator would need this overhead
> due to inheritance as it does occur even for base types.
>
> Btw, any idea why this overhead is added? Just curious. Thanks again
> for the reply.


Think about what information must be available when you delete[] the array...

-dr

  Réponse avec citation
Vieux 29/12/2007, 21h26   #7
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Placement new for Arrays : How does VC++ handle it ?

sparc wrote:

>
> Thanks. Yes. That is right. But I am just wondering why it adds for
> certain
> classes.


Well, the details are "magic" but the truth of the matter is that the
C++ allocation functions are designed to be as completely stupid as
the C malloc/free ones (as a matter of fact they are trivially
implemented as direct calls to those).

As a result you have the same stupidities that malloc suffers from.
There is no external interface to tell what size the allocation is
(even though the internal arena manipulation obviously has to know
this). So, in the case of an array of classes that have destructors,
the code needs to squirrel away the number of objects so it knows
how many objects to invoke destructors on.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h10.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17604 seconds with 15 queries