0

How can I use g_classPool to store dynamically allocated instances of class A. I should modify class A, that dynamically allocated instances are stored in the memory occupied by g_classPool. Can also someone tell me how can I learn such features. I was thinking to write void* operator new(...).

char g_classPool[1024];
class A
{
public:
    A(): m_data(42) {}
    ~A() {}
    int getData() { return m_data; }
private:
    int m_data;
};
int main()
{
    A* obj = new A();
    delete obj;
    return 0;
}

m_jdm35
  • 25
  • 1
  • 5
  • 3
  • 6
    My somewhat foggy crystal ball suggests you're wondering about [*placement new*](https://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new). It's probably wrong (it usually is, but I just keep using the damn thing). So perhaps elaborate more in your *question*. – WhozCraig Sep 13 '22 at 06:54
  • 1
    If you use static storage array to create other objects, it would not be a dynamic allocation. Which is the point, especially in embedded applications where non-deterministic time of allocation is a crucial problem. – Swift - Friday Pie Sep 13 '22 at 06:59
  • @Someprogrammerdude I suppose, legacy code. I have similar problem at hand in work project. Old code was written C-style and used POSIX sockets to communicate with everything. Some internal buffers for communication and internal event queues are created within statically declared arrays because there is always constant number of them, but there is code which treats them as `char*` – Swift - Friday Pie Sep 13 '22 at 07:07
  • Side note: use `std::byte g_classPool[1024];` if you have access to modern C++. – digito_evo Sep 13 '22 at 07:17
  • Could you specify a bit more? You want to store several instances, that is an array of A? A is the only object stored in the char array? `g_classPool` is a global variable and the only one, you want to use for storing? Do you know the alignment of the char array or does this have to be handled dynamically? – Sebastian Sep 13 '22 at 07:18
  • Can't use std::array or std::vector, I need the char array. I was hoping that knowing C will help. – m_jdm35 Sep 13 '22 at 07:50
  • 1
    You should look at [``](https://en.cppreference.com/w/cpp/header/memory_resource). I have never used it personally though. Essentially, it creates a buffer and then allows you to allocate from it and I think it's able to fallback to the global allocator as well, if I remember correctly. – asynts Sep 13 '22 at 07:53
  • [C++ Weekly - Ep 248 - Understand the C++17 PMR Standard Allocators and Track All the Things](https://youtu.be/Zt0q3OEeuB0) – asynts Sep 13 '22 at 07:55
  • I think it's meant for containers such as `std::vector` but you should be able to utilize the underlying memory resource classes directly. – asynts Sep 13 '22 at 07:56
  • Does this answer your question? [C++ "placement new" intricacies](https://stackoverflow.com/questions/46244126/c-placement-new-intricacies) – Sebastian Sep 13 '22 at 08:07
  • `void* operator new(size_t size) { void *mem = ::operator new (1,g_classPool); return mem; }' It seems like writing this works. And can I ask you what is going on? – m_jdm35 Sep 13 '22 at 15:27
  • Why do you even want to store the classes like this? It's difficult to respond if you don't provide sufficient context. Why not just put them on the heap and use `std::unique_ptr`? – asynts Sep 14 '22 at 06:15
  • @m_jdm35 You do not use `size`. Why redefine `new`? This is incompatible with `delete`! Several calls to your function store in the same location. If you know nothing about the alignment of the char array, it can possibly shoot you in the foot with types > 1 Byte. – Sebastian Sep 14 '22 at 07:26
  • Because it will be easy and they prefer to write void main(int argc, char* argv[]), instead of int main() return 0; (in c++ program). They are asking me to the stuff like this. No, no ints we write only unsigned. Because we fear that someone will read it. – m_jdm35 Sep 15 '22 at 07:37

0 Answers0