Re: Object layout guarantees and manual pointer adjustments
On 19 Apr., 10:58, Pavel Minaev <int...@gmail.com> wrote:
On Apr 18, 3:42 pm, Francis Glassborow
<francis.glassbo...@btinternet.com> wrote:
I cannot imagine how a system might work where the value of sizeof(T)
was the result of summing two or more non-contiguous chunks of storage.
I think the following code must always work for all types T (given
enough memory is available)
int main(){
void* mem_ptr = operator new( sizeof(T));
T* T_obj = new (mem_ptr) T;
}
Now explain, if you can, how that could work if the base memory for T
could be non-contiguous.
By allocating any additional memory during the evaluation of the
expression "new T", and not counting that memory for the purpose of
sizeof().
True. You could place a virtual base class suboject somewhere else
for example. But it would require a 2nd allocation that is either
done transparently behind the scenes or a via the global new operator
that might be a user-provided function. So, you either prevent the
user from taking control over allocation (which goes against the very
idea of making new/delete overloadable) or a user-declared global new
operator is called multiple times with varying size parameters for
just one new-expression which makes this behaviour observable.
I also fail to see the merit in allowing that. It seems it would make
an implementation only more complex without any advantages. If that's
true, adding these guarantees explicitly won't hurt.
Cheers!
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]