How to find the memory used by any object

后端 未结 5 2141
情书的邮戳
情书的邮戳 2020-12-09 16:04
class Help
{
public:
        Help();
        ~Help();

        typedef std::set Terms;
        typedef std::map &         


        
5条回答
  •  长情又很酷
    2020-12-09 16:39

    If you are looking for the full memory usage of an object, this can't be solved in general in C++ - while we can get the size of an instance itself via sizeof(), the object can always allocate memory dynamically as needed.

    If you can find out how big the individual element in a container are, you can get a lower bound:

    size = sizeof(map) + sum_of_element_sizes;
    

    Keep in mind though that the containers can still allocate additional memory as an implementation detail and that for containers like vector and string you have to check for the allocated size.

提交回复
热议问题