473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Thanks in advance
Venkatesh.SC
Dec 26 '07 #1
6 3612
On Dec 26, 11:06 am, sparc <sc.venkat...@gmail.comwrote:
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 help ?

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.
Dec 27 '07 #2
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
Dec 27 '07 #3
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).

Dec 28 '07 #4
On Dec 28, 8:35*am, Ron Natalie <r...@spamcop.netwrote:
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
Dec 29 '07 #5
On 2007-12-29 14:06:59 -0600, sparc <sc**********@gmail.comsaid:
On Dec 28, 8:35*am, Ron Natalie <r...@spamcop.netwrote:
>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

Dec 29 '07 #6
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.
Dec 29 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wenjie | last post by:
Hello, I read that the placement new looks like: void* SomeClass::operator new(size_t, void* location) { return location; } If for some reason, I overload the operator new as:
34
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
1
by: Kurt Richardson | last post by:
Hi all Sorry to bother you with what is probably a really trivial question for you C++ experts. My programming skill are pretty amateur, but I'm pretty good at VB.NET. However, I'm wanting to...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
11
by: letz | last post by:
Hi, We have a class whose objects are to be allocated in shared memory. To do that a ShmManager base class is defined so that operator new and delete are redefined to allocate segments in shared...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.