Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a guarantee that the address of the object is the same?

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);
    return res;
}

// using

int stack[32];
auto alloc = make_allocator(stack);
printf("addr outside: %p\n", alloc);

// 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Why that happens? And how can I get guarantees that will continue to happen?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading