All Questions
Tagged with c++ new-operator
1,699
questions
1
vote
0
answers
85
views
Difference in Memory Usage Between malloc and new in C++
I'm observing a difference in the total memory allocation when using malloc versus new in a simple C++ program, as reported by Valgrind. Below are the two versions of my program and the corresponding ...
0
votes
0
answers
47
views
How does the behavior of this override of placement new change things? [duplicate]
I just came across a usage of placement new that I don't understand, which I suppose is not surprising since I've never used it at all.
// Copy event type and map data reference from queue entry *...
1
vote
2
answers
103
views
C++: array types and new
I'm learning C++. The following program looks nice and generic:
typedef some_type T;
int main() {
T* p = new T;
}
And it indeed works for non-array types like int, etc. However, if I try typedef ...
0
votes
0
answers
31
views
Getting heap storage with proper alignment in C++ for overaligned type
In some use case, you'll need to allocate storage before creating objects inside this storage.
Then in order to create these objects, you may need to use placement new:
T *pobj = new(pstorage);
yet ...
-2
votes
1
answer
64
views
Getting heap storage with proper alignment in C++ for non-overaligned type
In some use case, you'll need to allocate storage before creating objects inside this storage.
Then in order to create these objects, you may need to use placement new:
T *pobj = new(pstorage);
yet ...
2
votes
1
answer
83
views
Correctly deleting a dynamically allocated plain array, allocated with `std::align_val_t` alignment parameter
As an answer to Idiom for initializing an std::array using a generator function taking the index?, I proposed a way to obtain a properly aligned storage.
Yet I think that my solution can be simplified,...
0
votes
1
answer
115
views
Can std::make_shared cowork with operator new?
It is said that
std::make_shared uses ::new, so if any special behavior has been set up using a class-specific operator new, it will differ from std::shared_ptr<T>(new T(args...)).
So, given ...
3
votes
1
answer
110
views
How to get total allocated size in custom operator delete[]?
I understand we can use Class-specific overloads of operator new[]/delete[] to specify our own memory allocation strategy.
So I write the following code. I barely want to use the held field to record ...
0
votes
0
answers
54
views
Error when calling new operator - malloc.c:2379
I am trying to create a dbus proxy using a MyDBUS library I have been assigned
The problem is, when I try to do new MyDBUS() the program stops and a malloc error comes out on the screen output, before ...
1
vote
0
answers
68
views
Using placement new on underaligned buffer in C++
Given a buffer
char buf[sizeof(T) + alignof(T) - 1];
Is calling placement new that constructs an instance of T on the pointer to the first element of buf that is located at an address which is a ...
3
votes
1
answer
92
views
C++ size of allocated array extraced memory close to returned pointer from malloc
I have lately been experimenting with overloading the new and delete operators, and I noticed something very interesing.
When I allocate, let's say, class T with new T(); vs new T[1](), there is a ...
2
votes
1
answer
246
views
How to resolve 'weak-def symbol not found' error run-time error for a C++23 program that uses std::println and omp?
Using std::print with OpenMP results in a runtime error and I am not sure which path to take in resolving the issue. Consider the code below:
/// clang++ -fopenmp -std=c++23 -Wall -Wextra -Werror -g ...
0
votes
2
answers
138
views
Can out-of-order execution of CPU affect the order of new operator in C++?
The new operator in C++ performs the following actions:
Allocates Memory: It allocates memory on the heap for a single object or an array of objects. The amount of memory allocated is enough to hold ...
0
votes
1
answer
78
views
Does this use of "new Mystring" create a memory leak? C++
Before anyone start saying that I should use std::string, this is an exercise part of a course, and I'm trying to learn here, not just to make something work.
The code runs ok, but I'm trying to ...
2
votes
3
answers
124
views
Is whether the placement new operator is solely responsible to the construct object
Let me share my Understanding on new operator. new operator do two jobs.
Allocate memory
construct memory at same place.
The new operator allocates memory using the global method::operator new(), ...