770 questions
-3
votes
0
answers
103
views
How to write delete correctly for the allocator [closed]
I have a solution where I need to write a custom allocator that uses a std::vector as storage for the objects allocated with it. I'm using a std::shared_ptr for the std::vector, and I know that it ...
2
votes
1
answer
112
views
Why does my custom global allocator crash on large allocations, and how can I handle alignment properly?
I am trying to implement a custom global allocator for use in my Rust application. The goal is to track memory usage and align to 64 bytes. My allocator uses libc::malloc and libc::free for allocation ...
0
votes
0
answers
88
views
Is on-demand creation of stateless allocator or deleted allowed by the standard?
Reading what is the new feature in c++20 [[no_unique_address]]? , I have been thinking about alternatives to Empty Base Optimisation prior to C++20's [[no_unique_address]]. One thing came to mind, ...
9
votes
1
answer
169
views
Is it allowed to call `deallocate` on a moved-from allocator (MSVC standard containers do)
When MSVC move-constructs an std::set, it also move-constructs the allocator. Later, when it destructs the moved-from set, it uses the allocator to deallocate an element. The following explains what ...
4
votes
2
answers
211
views
Why doesn't a container (e.g.) vector pass allocator to allocator-aware element-type?
I've a hard time understanding why a std container doesn't pass on its allocator to its allocator-aware elements. What are the details I miss here? Why isn't the allocator passed on, when - as I ...
4
votes
1
answer
118
views
std::list with a custom allocator crashes when removing items
I wrote a memory allocator to use with standard containers. However, every time I try to remove an element from a std::list, the program crashes at the line l.remove(elt).
I spent time investigating ...
0
votes
0
answers
38
views
C++ uses-allocator construction with allocator aware constructors - inconsistency between std::allocator and std::pmr::polymorphic_allocator
When I have an object which has allocator aware constructor, I can easily break the code when switching from std::allocator to std::pmr::polymorphic_allocator. It's beacause the polymorphic_allocator ...
5
votes
2
answers
269
views
Can I safely use uintptr_t in my arena allocator?
I made a really simple arena allocator in C and I was wondering one thing.
Here is my header file (by the way if this api does not seems right please tell me!).
#ifndef ARENA_H
#define ARENA_H
#...
2
votes
1
answer
124
views
C++17 std:map w/allocator gives compile error C2338 (VS 2022)
I use std::map with a simple compare function and an allocator.
Under C++17 with a map pair of std::pair<type1, type2>, I have learned that the allocator return type is required to be std::pair&...
0
votes
0
answers
50
views
why std::pmr::unsynchronized_pool_resource does not reuse the memory if there's a weak pointer to the allocated shared pointer?
I am playing around with the pmr.
I've got this simple code:
#include <print>
#include <memory_resource>
#include <memory>
#include <vector>
#include <string>
#include &...
1
vote
0
answers
117
views
What is the difference between the conversion (obj1*)(void*)p and (obj1*)p where p is a pointer to obj2 type
Background:
I encountered this in GCC c++ standard library extension pool_allocator.h, which contains an allocator type that utilize memory pool to improve the efficiency of small chunk memory ...
4
votes
2
answers
155
views
Given that allocators are copied by value how is allocator state shared?
I have implemented a custom allocator that takes a block of memory from the stack and allocates linearly out of it, ignoring calls to deallocate.
This is used with a std::map to improve performance in ...
3
votes
0
answers
171
views
Workaround for lack of destroying_delete_t in C++?
C++20 added the concept of "destroying delete" which lets you create overloads of operator delete that are required to run the object destructor themselves. Normally the destructor call is ...
1
vote
0
answers
75
views
C++: specialization of custom Allocator for use with std::allocate_shared
I need a custom Allocator to create share_pointer objects ultimately using a pool of dedicated memory. For some classes, however, I will need to further specialize the Allocators. These specialized ...
2
votes
1
answer
107
views
Is it possible to have a stack allocator that contains a buffer?
I'm trying to implement a stack allocator that would work with std::vector. There are plenty of already existing implementations like this or this. However, all of them assume the buffer is not a ...
6
votes
2
answers
408
views
Do C and C++ differ on the legality of aligning pointers at runtime?
Assuming alignment is a uintptr_t power of 2, looking for the next properly aligned address can be done using this expression:
(address + alignment - 1u) & ~(alignment - 1u)
This is used in ...
1
vote
1
answer
53
views
Efficiently returning local object allocated with external memory resource in boost::json
As part of a larger C++11 application, I have a function which creates a Boost JSON object and then returns it. One of the parameters to this function is a Boost JSON storage_ptr to an external ...
0
votes
1
answer
126
views
Why would a class take an std::allocator as an argument?
I am using an external library, and they have a particular convention with their classes that makes no sense to me. They will declare them like this:
class SomeClass
{
public:
using ...
1
vote
2
answers
178
views
Is it possible to override the default Allocator in C++ with out manualy specifying it in each constructor
C++ standard library containers such as std::vector accept an optional template parameter that lets you specify an alternate allocator. The examples I can find show this happening at each object ...
2
votes
1
answer
97
views
What is my allocator missing to be swappable
I'm still learning allocators and I've struggled with the error that occurs, for example, on
std::vector::shrink_to_fit() method call. Here is my allocator (I omitted some
(I believe) unimportant code ...
-1
votes
2
answers
106
views
Is it possible to make custom allocator to be acceptable by container constructor without specifying it in template arguments [duplicate]
Here is an example of allocator from cppreference:
#include <cstdlib>
#include <limits>
#include <new>
template<class T>
struct Mallocator
{
typedef T value_type;
...
0
votes
0
answers
72
views
What should template constructor of allocator do
I'm learning allocators and I have found that MSVC demand of presence of template constructor for allocator (and g++ and clang do not), here is minimal example of code that triggers error:
#include &...
-1
votes
1
answer
61
views
Handle submenu items from a drodown
I want to locate the elements from sub menu
i am trying to locate print but couldn't. how can i fix this?
My code
WebDriver driver = new FirefoxDriver();
driver.get("https://www.flipkart....
1
vote
1
answer
88
views
C++ rebind allocator with two template parameter
I'm writing an allocator that takes alignment as a template parameter also the alignment:
template<typename T, std::align_val_t alignment>
class AlignedAllocator {
public:
using value_type = T;...
1
vote
1
answer
111
views
Unordered_map with customized allocator compiler error
I am new to allocators. I am experimenting with a custom allocator used to allocate std::unordered_map and string:
#include <string>
#include <string_view>
#include <unordered_map>
...
7
votes
1
answer
176
views
What will happen if I call `allocate_at_least(0)` according to C++23 standard?
As shown here, the behavior of allocate(0) is unspecified.
So, what will happen if I call allocate_at_least(0) according to C++23 standard? Is the behavior implementation-defined, or will it be ...
2
votes
0
answers
95
views
How to implement simple allocator for use with STL containers?
I have an allocator, that fine works with std::vector, but has compile errors with std::basic_string. There is my code:
#include <vector>
#include <string>
#include <iostream>
...
2
votes
2
answers
267
views
Why C++ allocators use reinterpret_cast and how to avoid it?
I was trying to implement my own small allocator for testing purposes, and while designing it I thought that I don't know how to implement it without violating strict aliasing rule.
In most open-...
0
votes
1
answer
206
views
Why is a nested allocator in Rust causing heap corruption?
I've been experimenting with the Rust allocator_api feature and have developed a simple linear allocator. My test cases show this works as expected. However, I want to be able to nest allocators, e.g.:...
0
votes
1
answer
73
views
What is the problem when using a custom allocator in c++, when assigning the container to an iterator?
I have the problem that GCC is complaining about a type mismatch when compiling the following code:
#include <iostream>
#include <cstdint>
#include <vector>
#include <limits>
...
6
votes
1
answer
144
views
Why aren't container comparison operators allocator agnostic?
The comparison operators for containers require a matching allocator type, e.g. see the std::vector comparison operators.
That doesn't appear necessary to me… intuitively, all the operator needs to do ...
1
vote
2
answers
164
views
custom allocator and std::map
Here's the code i'm able to compile with gcc:
#include <iostream>
#include <map>
template <class T>
class map_alloc {
public:
typedef std::size_t size_type;
typedef std:...
15
votes
1
answer
600
views
Why std::allocator<T>::allocate calls ::operator new?
The documentation for the std::allocator<T>::allocate member function says in ([allocator.members]) that:
Remarks: The storage for the array is obtained by calling ::operator new ([new.delete]...
0
votes
0
answers
161
views
std::vector not free'ing memory [duplicate]
Under a specific access pattern, I have found a vector of vectors (std::vector<std::vector<Datum>>) might not free all its allocated memory when it is destructed.
For example, in the ...
4
votes
1
answer
170
views
Is std::launder needed after std::uninitialized_default_construct
I have a code similar to the following which uses an allocator to allocate raw memory, and then uses std::uninitialized_default_construct_n (or another function of the same family) to construct ...
2
votes
1
answer
90
views
Why can't I "destroy" CRTP vector that is "self-owned" but still can deallocate its address?
From Björn Fahller's Ligthning Talk at CPP Meeting 2023. => youtu.be/LKKmPAQFNgE
It's about how one can force c++ to leak memory without touching new or even malloc.
struct V : vector<V>...
0
votes
1
answer
302
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
94
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
69
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
2k
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
106
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
372
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
127
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
875
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
1
answer
117
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
215
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
58
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
42
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 <...
1
vote
1
answer
631
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
108
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 ...