By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,061 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,061 IT Pros & Developers. It's quick & easy.

Allocators, I don't know what I'm talking about.

P: 1
Hello Der,

I'm trying to make a ECS in c++ and started exploring the existing material a little, going down the rabbit hole I ended up finding out about special allocators.

I think I get the theory behind the allocators, and have seen multiple (around 5) videos from cpp con and one from code::dive 2018 about allocators, and I do get that using allocators<typename> is bad and that you should use std::pmr::datatype<typename, &(allocator)> instead.

What I specifically don't understand is what function other than : new, malloc, realloc, calloc (these are general allocators right?). do you use to get the memory in the first place? Like what is that one function that gives you a block of memory on the ram to work with so that nothing else touches it? What does the actual grabbing memory out of thin air work?

Do you like just get an array of some required size in bytes, and then deal with that ? Or is there a function that you can call so you don't have to create an array? This seems kind of contradictory?

I apologize if I make absolutely no sense, but can someone please help me out... I don't think I'm intelligent enough to figure this out on my own...
usps tracking showbox speed test
3 Weeks Ago #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,949
malloc, realloc, calloc*are C functions, in general you should not use them in C++.

You should be using new, the major difference is that malloc, realloc, calloc*allocates you a block of uninitialized memory where as new allocates the block of memory and then calls the appropriate constructor on it to initialize the memory.

std::pmr is all very well but it is C++17 and there is plenty of development still going on using C++11 and C++14 so you need to know how to use new correctly.
3 Weeks Ago #2

Expert 100+
P: 260
What I specifically don't understand is what function other than : new, malloc, realloc, calloc (these are general allocators right?). do you use to get the memory in the first place?
General-purpose languages provide the basic functions/keywords to be used to make software in the widest variety of application domains. Custom data structures can be made according to the needs by making use of the basic entities.

Like what is that one function that gives you a block of memory on the ram to work with so that nothing else touches it? What does the actual grabbing memory out of thin air work?
When you reserve a memory space using a variable, array, dynamic allocation, etc., it is untouchable by any program/process in regards to reallocation. No other process can reallocate that memory to something else unless the cell spaces are freed and are no more tagged as "in use". If it could, there would be no concept of memory leakage. However, that block of memory can be manipulated via manual programming using pointers. That's why pointers need to be dealt with care. Languages like Java don't make use of the pointers directly for security purposes.
3 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.