placement-new Questions
2
This is a follow-up to my previous question where I seem to have made the problem more involved than I had originally intended. (See discussions in question and answer comments there.)
This questio...
Psychosomatic asked 8/12, 2019 at 19:24
1
Is it allowed to reuse storage of a non-static data member and if so under what conditions?
Consider the program
#include<new>
#include<type_traits>
using T = /*some type*/;
using U ...
Ivonneivor asked 8/12, 2019 at 16:57
2
Solved
I know that this question was asked several times already but I couldn't find an answer for this particular case.
Let's say I have a trivial class that doesn't own any resources and has empty dest...
Miniaturize asked 29/10, 2019 at 12:39
2
Solved
[basic.indet] p1 says:
When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the obj...
Shaum asked 12/10, 2019 at 16:12
1
Solved
Consider the following example:
#include <iostream>
struct A {
int i;
A(int i)
{
this->i = i;
}
A &operator=(const A &a) = delete;
A(const A &a) = delete;
};
int m...
Bisector asked 7/10, 2019 at 18:9
1
Solved
When using placement new in generic code to construct an object at a specified address, the usage pattern is a bit different from usual code. For example, consider this implementation of uninitiali...
Mcferren asked 17/8, 2019 at 19:43
3
I have an object which I want to 'transform' into another object. For this I am using a placement new on the first object which creates a new object of the other type on top of its own address.
Co...
Regenerative asked 16/4, 2019 at 15:0
6
Solved
C++17 adds std::destroy_at, but there isn't any std::construct_at counterpart. Why is that? Couldn't it be implemented as simply as the following?
template <typename T, typename... Args>
T* ...
Coates asked 24/10, 2018 at 10:23
1
Solved
If memory is allocated with malloc (as opposed to new) and an object is moved into that memory, is that valid C++?
Let's say I allocate memory for an array of n objects of type T, and I have a rang...
Balsam asked 9/2, 2019 at 1:41
1
Regarding this code:
#include <string>
int main()
{
union u {
u() { i = 0; }
~u() {}
int i;
std::string s1;
std::string s2;
} u;
new (&u) std::string{};
}
[intro.object]/2 s...
Kravits asked 17/1, 2019 at 13:25
4
Solved
In C one can allocate dynamic arrays using malloc(sizeof(T) * N) and then use pointer arithmetic to get elements at i offset in this dynamic array.
In C++ one can do similar using operator new() i...
Iy asked 23/11, 2018 at 18:59
1
Solved
C++17 adds std::uninitialized_move, but there is no std::uninitialized_move_if_noexcept that would use std::move_if_noexcept internally. In my opinion, it would be useful, since now, if we want to ...
Fixity asked 23/10, 2018 at 7:20
2
Is it defined behavior to placement-new a trivially destructible base object of a derived?
struct base { int& ref; };
struct derived : public base {
complicated_object complicated;
derived(i...
Transpontine asked 19/10, 2018 at 0:18
2
Solved
While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows:
template <typename T>
class optional {
// ...
...
Gritty asked 14/9, 2018 at 19:34
4
Solved
I'm trying to figure out whether the following is undefined behaviour. I have a feeling it's not UB, but my reading of the standard makes it look like it is UB:
#include <iostream>
struct A...
Firsthand asked 3/9, 2018 at 16:44
2
Solved
I'm working on some memory space with custom allocation and deletion, which are made using a malloc-like interface, that is not under my control (i.e. opaque C-style functions for "allocate n ...
Clubbable asked 7/11, 2016 at 19:16
3
Solved
This question is to confirm I understood the concept right and take expert opinion on the style of usages and possible optimization.
I am trying to understand "placement new" and following is the ...
Hexapody asked 29/1, 2016 at 14:44
2
Solved
I know generally it's impossible to reset a reference after it's already initialized.
However, I somehow try out the following code and it happens to work on both clang++ and g++.
My question is,...
Clarabelle asked 26/4, 2018 at 9:43
1
Solved
What is the <- operator/expression in Rust? You can find the symbol here.
I happened to be looking at a page describing expressions and operations in Rust. I do not program in Rust, so I asked ...
Nobie asked 10/4, 2018 at 20:3
1
Solved
This question originates from the comment section in this thread, and has also got an answer there. However, I think it is too important to be left in the comment section only. So I made this Q&...
Hueyhuff asked 30/3, 2018 at 5:25
1
Solved
I have the following code:
#include "type_traits"
#include <new>
void foo()
{
std::aligned_storage<10,alignof(long)> storage;
new (&storage) int(12);
}
I have some storage de...
Fullmer asked 21/3, 2018 at 20:5
1
Solved
During a dive into dynamic memory, it occurred to me it appears contradictory how trivial types begin their lifetime. Consider the snippet
void* p = ::operator new(sizeof(int)); // 1
// 2
new (p) ...
Murex asked 28/2, 2018 at 20:46
4
Solved
I'm starting to use CUDA at the moment and have to admit that I'm a bit disappointed with the C API. I understand the reasons for choosing C but had the language been based on C++ instead, several ...
Smelt asked 18/11, 2008 at 18:59
2
I saw a presentation on cppcon of Piotr Padlewski saying that the following is undefined behaviour:
int test(Base* a){
int sum = 0;
sum += a->foo();
sum += a->foo();
return sum;
}
int B...
Dunlap asked 9/2, 2018 at 13:54
2
Please see the below code:
unsigned char* p = new unsigned char[x];
CLASS* t = new (p) CLASS;
assert((void*)t == (void*)p);
Can I assume (void*)t == (void*)p?
Verbose asked 9/1, 2018 at 7:57
© 2022 - 2025 — McMap. All rights reserved.