How to find the memory used by any object

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

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


        
5条回答
  •  萌比男神i
    2020-12-09 16:44

    Short Answer: No

    Long Answer:
    -> The basic object yes. sizeof() but this is only useful for limited things.
    -> A container and its contained members: NO

    If you make assumptions about the structures used to implement these objects you can estimate it. But even that is not really useful ( apart from the very specific case of the vector).

    The designers of the STL deliberately did not define the data structures that should be used by these containers. There are several reasons for this, but one of them (in my opinion) is to stop people making assumptions about the internals and thus try and do silly things that are not encapsulated by the interface.

    So the question then comes down to why do you need to know the size?
    Do you really need to know the size (unlikely but possible).

    Or is there a task you are trying to achieve where you think you need the size?

提交回复
热议问题