简体   繁体   中英

memory management & std::allocator

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:

  • 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)
  • What does the std::allocator do? - Does it declare from the stack, or from the heap?
  • (follow up from previous question): if I copy an item declared in the stack to the datastructure is it still declared in the heap?

Thanks again, paul23

  • 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.

Your answers:

  • Yes, you are correct. Anyway, if the template type of the container is a pointer, as in your case, the container will allocate/deallocate memory for the pointer, not for the object which the pointer is pointing to!

  • It's up to the implementation, in the $20.2.5 of the standard are described the allocators' requirements.

  • Since you are storing pointers, I fear the answer to this question would confuse you. If you copy in the container a pointer to an object that's allocated in the stack, your object will remain in the stack. Once you get out of the scope, it will get destroyed and you will have an invalid pointer in the container.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM