placement-new Questions
10
Solved
Can I call constructor explicitly, without using new, if I already have a memory for object?
class Object1{
char *str;
public:
Object1(char*str1){
str=strdup(str1);
puts("ctor");
puts(str);
...
Rachaba asked 22/3, 2010 at 17:44
1
Solved
P0593, under the Type punning section, presents this example:
float do_bad_things(int n) {
alignof(int) alignof(float)
char buffer[max(sizeof(int), sizeof(float))];
*(int*)buffer = n; // #1
new...
Firewater asked 7/9, 2023 at 10:43
0
This is a basic example of using a dynamic unsigned char array as a "storage" for a type T.
unsigned char* storage = new unsigned char[sizeof(T)];
T* foo = new(storage) T; // line 2
// us...
Bryozoan asked 1/9, 2023 at 14:26
8
Solved
Unlike new and delete expressions, std::malloc does not call the constructor when memory for an object is allocated. In that case, how must we create an object so that the constructor will also be ...
Hindustan asked 8/6, 2010 at 6:2
5
Solved
This is a minimal working example for the problem I am facing in my real code.
#include <iostream>
namespace Test1 {
static const std::string MSG1="Something really big message&q...
Inspection asked 1/3, 2023 at 7:10
2
Let S be a struct type which contains a character array data which has the maximum alignment and a fixed size. The idea is that S is able to store any object of type T whose size does not exceed th...
Wastage asked 15/4, 2018 at 11:46
3
There seems to be some agreement that you can't willy nilly point (an int*) into a char array because of the C++ aliasing rules.
From this other question -- Generic char[] based storage and avoidi...
Princely asked 12/1, 2017 at 23:8
3
According to [expr.const]/5.18:
An expression E is a core constant expression unless the evaluation of E, following the rules of the abstract machine ([intro.execution]), would evaluate one of the...
Minaminabe asked 25/9, 2022 at 8:56
1
Solved
I wonder if this code is legal:
delete new (operator new(1)) char;
This code does the same thing, but doesn't use delete expression on a pointer obtained from placement new:
void* p = operator new...
Fowling asked 19/8, 2022 at 14:50
4
I am learning about placement-new in C++ using the books listed here. Now, to look at some examples, I came across the following snippet in one of the SO post that claims that it (the given example...
Drusilla asked 3/7, 2022 at 12:11
3
Solved
There are two existing questions about replacing vector elements that are not assignable:
C++ Use Unassignable Objects in Vector
How to push_back without operator=() for const members?
A typica...
Midweek asked 16/10, 2012 at 5:58
1
This is a followup on the answers for placement new on a class with reference field.
Calling std::vector<A>::data() on type A that has reference or const fields, returns a pointer to objects ...
Rasbora asked 10/6, 2020 at 8:15
2
Solved
struct A
{
int x;
}
A t{};
t.x = 5;
new (&t) A;
// is it always safe to assume that t.x is 5?
assert(t.x == 5);
As far as I know, when a trivial object of class type is created, the compil...
Hue asked 1/3, 2022 at 14:20
0
std::launder has a precondition that all bytes reachable from the would-be-returned pointer are reachable through the passed pointer.
My understanding is that this is meant to allow compiler optimi...
Foreleg asked 21/1, 2022 at 20:43
1
When emplace_back() is called on std::vector instance, an object is created in a previously allocated storage. This can be easily achieved with placement-new, which is perfectly portable.
But now, ...
Lardaceous asked 29/6, 2020 at 16:25
2
Solved
Suppose there's a struct whose constructor does not initialize all member variables:
struct Foo {
int x;
Foo() {}
}
If I memset some buffer to 0, use placement new on that buffer to create an in...
Shoveler asked 9/8, 2021 at 14:13
1
To clarify, is the following program well-formed?
#include <new>
char foo[32];
struct bar {
static constexpr int foobar = 42;
};
int main()
{
auto p = new (foo) bar();
static_assert(p-&g...
Asdic asked 4/7, 2021 at 19:24
2
Solved
Consider this code:
void f(char * ptr)
{
auto int_ptr = reinterpret_cast<int*>(ptr); // <---- line of interest
// use int_ptr ...
}
void example_1()
{
int i = 10;
f(reinterpret_cas...
Lebron asked 6/7, 2018 at 6:38
7
Solved
5.3.4 [expr.new] of the C++11 Feb draft gives the example:
new(2,f) T[5] results in a call of operator new[](sizeof(T)*5+y,2,f).
Here, x and y are non-negative unspecified values representing arra...
Sugden asked 4/1, 2012 at 0:3
25
Solved
Has anyone here ever used C++'s "placement new"? If so, what for? It looks to me like it would only be useful on memory-mapped hardware.
Mealtime asked 21/10, 2008 at 16:34
3
Solved
Consider the following example:
#include <new>
struct FunctionObject
{
int operator()() // non-const, modifies the state of this object
{
i += 1;
j += 2;
return i + j;
}
int i = 0;
...
Toggle asked 20/10, 2020 at 15:49
2
The C++ standard specifically bans calling new in a constant expression (N4296 section 5.20 [expr.const]):
A conditional-expression e is a core constant expression unless the evaluation of e, foll...
Operate asked 10/1, 2017 at 23:5
2
Solved
I know this is a pretty common subject, but as much as the typical UB is easy to find, I did not find this variant so far.
So, I am trying to formally introduce Pixel objects while avoiding an act...
Soldier asked 14/1, 2020 at 16:17
1
Solved
class FooView final : public Something
{
...
void refresh()
{
this->~FooView();
new (this) FooView();
}
}
I have never seen this idiom, and it seems like it could be really subtle and me...
Quahog asked 10/1, 2020 at 21:33
2
The "undead" clause
I call the undead clause the C++ rule that after the destruction of an object, if a new object is created at the same address, it can sometimes be considered the same object as...
Olericulture asked 12/12, 2019 at 6:31
1 Next >
© 2022 - 2024 — McMap. All rights reserved.