In reviewing my code I see some "ugly" structure I use, in a class (called "map") I have a vector which contains a "data" class:
std::vector<PointerToHUGEClass> vector;
Where PointerToHUGEClass is just like the name describes. (though the object pointed too is also owned by the map class, and created with the "new" parameter in the constructor). This works all good (at the moment). However I still feel it is more of a work-around.
The only reason I am using a "PointerToHUGEClass" instead of just "HUGEClass", is because I wanted to make sure the object is not declared from the stack. This was made however before I understood allocaters. Now I feel it is more or less the task of the allocator to ensure the memory isn't declared from the stack.
My questions:
Thanks again, paul23
Memory management is the process of controlling and coordinating a computer's main memory. It ensures that blocks of memory space are properly managed and allocated so the operating system (OS), applications and other running processes have the memory they need to carry out their operations.
Memory management is the functionality of an operating system which handles or manages primary memory and moves processes back and forth between main memory and disk during execution. Memory management keeps track of each and every memory location, regardless of either it is allocated to some process or it is free.
It keeps track of every memory location(if its is free or occupied). Paging, and swapping,segmentation and compaction are modern computers' four main memory management techniques. Swapping is the best technique for memory management because it provides the most efficient use of system resources.
- Am I correct in assuming the allocator is responsible for the memory management from the items? (And making sure it is declared from stack/freestore/heap/whatever)
No you are not. The allocator is just sugar coating over new
and delete
and in general responsible of deciding where the memory will be allocated. The responsibility of calling allocate
, deallocate
, construct
and destruct
is to its users (which means here, std::vector
). From your point of view, it'll be automatic, which is what matters here after all.
- What does the std::allocator do? - Does it declare from the stack, or from the heap?
std::allocator
is mandated to allocate using ::operator new(size_t)
, thus it depends on the definition of the global new
operator. Generally, this means the heap. The stack is for object with automatic storage duration.
- (follow up from previous question): if I copy an item declared in the stack to the datastructure is it still declared in the heap?
If you copy an item, then a copy is allocated where you copy it. Here it means copying an item from the stack to the heap. You then have two copies of the object, meaning that changes on one copy are not reflected on the other.
Beware though, that we are talking about the default copying mode, that is a shallow copy. If you copy an object, it'll get copied all fine; if you copy a pointer, only the pointer will be copied, not the data pointed to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With