I have some base class which holds a pointer. Also I have two derived classes which are implemented as stack-allocated and dynamically allocated storage. From those classes I need to set the pointer on the base class, for the first case - to the address of a stack allocated storage, and for the second - for dyn-allocated space.
My question is for stack-allocated case. Example:
struct base { int *ptr; };
template<std::size_t N>
struct stack_allocator: base {
int arr[N];
};
struct dyn_allocator: base {
};
template<std::size_t N>
auto make_allocator(int (&arr)[N])
-> stack_allocator<N> // because of C 11
{
stack_allocator<N> res;
res.ptr = res.arr;
printf("addr inside: %p\n", res.ptr);
return res;
}
// using
int stack[32];
auto alloc = make_allocator(stack);
printf("addr outside: %p\n", alloc.ptr);
// will print the same address
I do know that make_allocator()
can return just a copy with diff object address, but as far as I can see at least on my test and at least for now - the address is the same.
Why that happens? And how can I get guarantees that will continue to happen?
CodePudding user response:
The address of res
is the same as alloc
probably because of NRVO or other optimizations. I would not recommend relying on this behavior.
An easy way to achieve it portably is either by making make_allocator
a constructor of stack_allocator
, or by adding a reference / pointer parameter to make_allocator
in place of the return value.