Questions tagged [allocator]
A component of C++'s Standard Library, in charge of handling the requests for allocation and deallocation of memory for a container.
735
questions
0
votes
1
answer
63
views
If C++17 and above guarantee that allocators must support overaligned types, does that mean we can avoid creating manually-aligned types?
Given a custom vector and using std::allocator for allocating, under C++17 and above do we still need to create an internal overaligned type OT using alignas, then allocate for OT, and ...
2
votes
1
answer
46
views
Opening Boost Interprocess segment in Constructor of object
I create a map<int, vector<int>> in a Boost Interprocess managed_shared_memory in one process and then want to open it in another process to write to it continually.
The other process ...
0
votes
0
answers
54
views
Create std-compliant allocator that wraps arbitrary allocator for diagnostics
As part of a unit test suite for custom std-compatible containers, I want to do some memory tracking. The easiest thing is to define a new allocator and track allocations and deallocations with it, ...
2
votes
1
answer
208
views
How Can I Specify an Allocator at Comptime in Zig?
In Zig, I want specify an allocator at comptime in order to remove the added overhead of passing around an allocator to each function that requires it. In the following MWE, I have a type generator ...
0
votes
0
answers
69
views
Moving elements between two std::vectors which are using different allocators
I have an object of type std::vector<a_trivially_copiable_type, my_custom_allocator>. At some point, I need to inject this into a 3rd-party-library which accepts only a std::vector<...
1
vote
2
answers
75
views
Why is propagate_on_container_move_assignment not applicable for copy constructors of a container
I have read some old posts on propagate_on_container_move_assignment to understand how it works but still not able to wrap my head around some of the finer points. As per my understanding, move ...
6
votes
1
answer
83
views
Is allocators' allocate and construct well-defined through [basic.life]p8?
cppreference's example of std::allocator contains this code (shortened for simplicity):
// default allocator for ints
std::allocator<int> alloc1;
using traits_t1 = std::allocator_traits<...
0
votes
2
answers
199
views
custom allocator for std::unordered_map
I am trying to use my custom allocator for a std::unordered_map. The allocator already works for my own objects and also for std::vector, but when I try to use it in the same way for a std::...
4
votes
2
answers
82
views
Do rebound allocators have the same size_type and difference_type?
After reading a very in-depth article on fancy pointers, where it is also described their correlation with allocators, I had a doubt. The paper covers several aspects of fancy pointers, but does not ...
0
votes
1
answer
71
views
monotonic_buffer_resource and exception handling
The monotonic_buffer_resource works a bit like a stack that only supports push but not pop. Unfortunately I see no way howto revert to its previous state when catching an exception.
Even when handing ...
0
votes
0
answers
27
views
Where does it says in the Python CAPI that if `__new__` does not return the class or an instance of it, `__init__` will not be called?
In the Python's documentation, it is said that if the __new__ method of the class does not return either the class itself or an instance of it, the corresponding __init__ method will not be called.
...
0
votes
0
answers
40
views
Leakage message from sanitizer for program that does not allocate anything
I have some code that does almost nothing, but it does so in a convoluted way. Most importantly, it does not allocate anything. All data is on the stack. It works just fine with many versions of gcc ...
2
votes
0
answers
36
views
Are allocators required to be interconvertible?
I wondered for a long time how it is possible to assign an allocator of type A to an object expecting an allocator of type B. Check the following code:
Demo
#include <iostream>
#include <...
0
votes
1
answer
145
views
memory pool in thrust execution policy
I am looking for solutions to use a memory pool within thrust as I want to limit the number of calls to cudaMalloc.
device_vector definitely accepts an allocator, but it's not so easy to deal with ...
1
vote
1
answer
33
views
How to expand std::uses_allocator_construction_args into constructor parameter list?
I want to create a class Entity that can be templated for owning and non-owning string types (e.g. std::string or std::string_view). In both cases though there is internal datastructure to be ...
2
votes
1
answer
47
views
Conditionally allow supply of allocators to wrapped objects
I'm creating a container that can be templated on the string type it uses for its members, either it's an owning or non-owning string (std::string vs std::string_view). For the case of the non-owning ...
2
votes
1
answer
53
views
Constrain function argument to allocators using C++20 concepts
I wanted to constrain a constructor to take only allocators as arguments and I came up with this concept:
Demo
#include <iostream>
#include <string>
#include <memory>
#include <...
2
votes
1
answer
52
views
Allocator type inferred as const char* for some reason
I have an URL class that takes a templated StringType to store data. The allocator may also be provided using the Allocator template argument, if not it is inferred from the StringType. However: This ...
2
votes
1
answer
75
views
How to store (empty) allocator in my container without taking up size?
I have an allocator aware container that has to store the allocator if it is a stateful one (e.g. polymorphic allocator). Otherwise, the allocator should take up no space. I thought the stdlib would ...
1
vote
2
answers
120
views
Authoring a container to work with both c++11 and pmr allocators
How do I correctly create a container that works with both, C++11 and C++17 polymorphic allocators? Here's what I have so far (as a generic boilerplate template):
Explanation: I've included two fields,...
0
votes
1
answer
216
views
how to write a custom allocator for std::map in C++20
As I understand from cppreference we need to define rebind_alloc for our own custom allocator. But I did not really understand how I can let my own allocator also valid for the nodes of my map.
What I ...
1
vote
2
answers
74
views
Is it possible to bundle "runtime" allocator information with an allocator in C++?
When we supply an allocator for std::vector in C++, note that we supply an allocator class. A class cannot be created at runtime.
However, what if I need my allocator to depends on runtime information?...
2
votes
1
answer
115
views
If I overload the global new operator to use a memory pool to allocate memory, will this overloaded 'new' affect STL memory allocation?
Currently a project in my company overloaded the global new/delete.
I test for some short code, with the global operator new overloaded. And I see the overloaded new is called when I use STL(like ...
4
votes
2
answers
304
views
How does std::vec::shrink_to_fit work in Rust?
Does std::vec::shrink_to_fit allocate a new, smaller vec.len() buffer of data, copy to it, and destruct the old buffer, or does it somehow indicate to the memory allocator that the uninitialised part ...
0
votes
1
answer
53
views
How does one deal with different types when using custom allocated strings?
I have started experimenting using custom allocators with std::basic_string in a video game project. This is my first time dealing with custom allocators, so I'm learning a lot.
But the problem is, I ...
0
votes
1
answer
121
views
Base class has inacessible destructor
The following code tries to define a self-similar datastructure JSON using the std containers std::vector and std::unordered_map but does not compile. It all seems to come down to std::unordered_map's ...
2
votes
1
answer
54
views
Allocator for type that allocates itself
So I got this recursive variant that holds the types std::vector and std::unordered_map which again hold the type itself.
Now this should work according to the standard because:
std::unordered_map is ...
0
votes
0
answers
119
views
When should propagate_on_container_copy_assignment and propagate_on_container_move_assignment be true?
I am trying to study allocators, but I do not understand at all the uses of some allocator's aliases. In particular, I have already read in cppreference the consequences of the ...
0
votes
0
answers
22
views
Which type of open source allocator does newlib use?
I know there exists some versions of malloc implementation like ptmalloc, used by glibc, and jemalloc, used by FreeBSD.
I wonder which version of malloc implementation is adopted by newlib? Or the ...
1
vote
1
answer
2k
views
C++(3364) : operator -> or ->* applied instead of pointer type
I have declared and populated a map : std::map<std::string, std::vector<std::pair<std::string, std::vector<std::vector<float>>>>> data;.
I have a print function to print ...
1
vote
2
answers
181
views
i can't find the allocator.destroy() member function in xcode c++
I have a MacBook Air and I'm using Xcode as my IDE. When I write the code below, Xcode shows a warning that says "no member named destroy and construct in std::allocator<int>". However,...
1
vote
1
answer
114
views
call allocate() again on the released unsynchronized_pool_resource is an undefined behavior or not?
I am currently leaning the behavior of unsynchronized_pool_resource. So I have this sample code
std::pmr::monotonic_buffer_resource buffer_resource{std::pmr::new_delete_resource()}; //monotonic ...
2
votes
1
answer
94
views
what is the usage of the template parameter of the std::pmr::polymorphic_allocator?
I'm currently learning the polymorphic allocator, I watched some videos and trying to understand it. If I have this code:
class MyClass {
public:
MyClass(int val = 0) : m_val(val) {
std::...
-1
votes
1
answer
89
views
Correct way to add a pointer to an empty std::list using push_back
I have two simple classes representing nodes and edges in a connected graph structure.
//node.h
#include "edge.h"
#include <string>
#include <list>
#include <utility>
...
0
votes
1
answer
68
views
Can a vector hold two allocators or use different allocators to allocate and construct/destroy?
I'm trying to implement my custom vector. In some case, I decide to place capacity in front of the real data, and access capacity by shifting the pointer.
Assuming that sizeof(capacity) == 4 while ...
0
votes
1
answer
507
views
Custom C++ Allocator cause error in : _Container_base12::_Orphan_all_unlocked_v3()
So I intend to use this stack-based allocator for std::vector, and I use 2 arrays for the allocation(because vectors grow and copy the old buffer to the new).
Here's the full code:
#include <stdio....
0
votes
0
answers
129
views
Estimate the memory needed for pmr pool resource
I am learning about the std::pmr::unsynchronized_pool_resource and tried using it in a simple example.
In my example, I'm using a buffer l_Buffer with an arbitrarily determined size.
Is it possible to ...
2
votes
0
answers
150
views
String using pmr null memory resource
I am learning about the null_memory_resource and tried using it in a simple example. I set it as the default memory resource with set_default_resource and attempted to initialize a std::vector, but ...
6
votes
1
answer
1k
views
What are the differences between Block, Stack and Scratch Allocators?
In his talk "Solving the Right Problems for Engine Developers", Mike Acton says that
the vast majority of the time, all you're going to need are these three types of allocator: there's the ...
3
votes
2
answers
94
views
Should pmr containers with different allocators compare equal?
Say I have a container that uses a hardcoded pmr allocator to store data. What is the consensus among the C++ community: should operator==() compare equal on objects that only differ by their ...
2
votes
2
answers
66
views
Why does the STL overload their constructors with/without allocator argument?
in std::vector, why does the stl overload the constructors like that:
vector( const vector& other );
vector( const vector& other, Allocator& a );
Why not just do this?
vector( const ...
0
votes
3
answers
334
views
Allocator and STL containers
I'm trying to reproduce the std::vector container, and I saw that in one of the vector constructor prototype we can give an allocator.
explicit vector (size_type n, const value_type& val = ...
0
votes
0
answers
50
views
Use pmr objects as default method arguments (how to initialize allocator?)
Here's a contrived example of what I'm trying to do: I want to have a member function which takes a std::pmr::string as an argument but if the user doesn't provide it it should be initialized to the ...
0
votes
1
answer
158
views
std::fill crash with a empty std::string
I cannot understand why this code segfault. I allocate enough space for n element of a giving type with an allocator, and then fill the space with a copy of the default constructed type with std::fill....
1
vote
0
answers
51
views
How to allocate a variably sized object with correct alignment using an Allocator?
Say I have the following object that I want to be variably sized:
struct VarSized {
std::array<int, 500> fixedHeader;
std::byte extraMemory[]; // variably sized part
}
How can I ...
0
votes
1
answer
83
views
Custom STL Allocator: Construct/Destroy called for each element
I have this code snippet
auto start = high_resolution_clock::now();
std::vector<char> myBuffer(20e6);
std::cout << "StandardAlloc Time:" << duration_cast<milliseconds&...
1
vote
0
answers
51
views
Why is max_size called twice in C++ allocator?
I was playing around with allocators and I discovered that max_size is called twice each time for vector. Why is that?
#include <iostream>
#include <memory>
#include <vector>
#...
0
votes
0
answers
109
views
aligning memory in custom c allocator
I cannot seem to find an answer to my question. I am building a memory allocator. Let's say that the user mallocs 20 bytes of memory. I am storing some memory for the header and the 20 byte payload ...
5
votes
2
answers
239
views
If allocators are stateless in C++, why are functions not used to allocate memory instead?
The default std::allocator class is stateless in C++. This means any instance of an std::allocator can deallocate memory allocated by another std::allocator instance. What is then the point of having ...
2
votes
1
answer
130
views
How to define a custom mock allocator?
I'm trying to define a custom std::basic_string specialization with a mock allocator to log all memory operations that basic_string performs.
struct MockAllocator : std::allocator<char> {
...