C++ 计数动态分配的问题。C++;

C++ 计数动态分配的问题。C++;,c++,memory-management,C++,Memory Management,嘿,我已经写了代码,我不知道为什么它不工作,它假设要计算内存保留的数量,但我做了一些错误的事情(我的意思是两个计数器中的内存分配数量都等于0),我无法发现问题,我将感谢任何帮助。请先在这里发帖,请耐心等待D #include <iostream> #include <vector> using std::cout; using std::endl; struct A { int a; static int nr; void * operator n

嘿,我已经写了代码,我不知道为什么它不工作,它假设要计算内存保留的数量,但我做了一些错误的事情(我的意思是两个计数器中的内存分配数量都等于
0
),我无法发现问题,我将感谢任何帮助。请先在这里发帖,请耐心等待D

#include <iostream>
#include <vector>
using std::cout; using std::endl;
struct A
{
    int a;
    static int nr;
    void * operator new[](std::size_t n) {++nr; return ::new char[n]; }
};
struct B
{
    double b;
    static int nr;
    void * operator new[](std::size_t n) {++nr; return ::new char[n]; }
};
int A::nr = 0, B::nr = 0;
int main()
{
    std::vector<A> vecA;
    std::vector<B> vecB;
    for (int i{}; i < 1000; i++)
    {
        vecA.push_back(A());
        vecB.push_back(B());
    }
    cout << "Size of vecA: " << vecA.size() * sizeof(A) << ", number of times that memory was allocated: " << A::nr << endl;
    cout << "Size of vecB: " << vecB.size() * sizeof(B) << ", number of times that memory was allocated: " << B::nr << endl;

    return 0;
}
#包括
#包括
使用std::cout;使用std::endl;
结构A
{
INTA;
静态int-nr;
void*运算符new[](std::size_t n){++nr;return::new char[n];}
};
结构B
{
双b;
静态int-nr;
void*运算符new[](std::size_t n){++nr;return::new char[n];}
};
int A::nr=0,B::nr=0;
int main()
{
向量向量向量机;
std::vecB;
对于(int i{};i<1000;i++)
{
向后推(A());
向量B.推回(B());
}

cout要计算内存重新分配的数量,我只看到创建自己的分配器类。类似于:

template <typename T>
class countingAllocator : public std::allocator<T>
{
public:

   template<typename _Tp1>
   struct rebind
   {
      typedef countingAllocator<_Tp1> other;
   };

   T* allocate(size_t n, const void *hint = 0)
   {
      T::nr++;
      return std::allocator<T>::allocate(n, hint);
   }

   countingAllocator() : std::allocator<T>()
   { }

   countingAllocator(const countingAllocator &a) : std::allocator<T>(a)
   { }

   template <class U>
   countingAllocator(const countingAllocator<U> &a) : std::allocator<T>(a)
   { }
   ~countingAllocator()
   { }
};

// Fix for VS Debug build Don`t need for Release
template <>
class countingAllocator<std::_Container_proxy> : public 
  std::allocator<std::_Container_proxy>
{
public:
   template <class U>
   countingAllocator(const countingAllocator<U> &a) : 
     std::allocator<std::_Container_proxy>(a)
   { }
};


std::vector<A, countingAllocator<A>> vecA;
std::vector<B, countingAllocator<B>> vecB;
for (int i{}; i < 1000; i++)
{
    vecA.push_back(A());
    vecB.push_back(B());
}

要计算内存重新分配的数量,我只看到创建自己的分配器类。类似于:

template <typename T>
class countingAllocator : public std::allocator<T>
{
public:

   template<typename _Tp1>
   struct rebind
   {
      typedef countingAllocator<_Tp1> other;
   };

   T* allocate(size_t n, const void *hint = 0)
   {
      T::nr++;
      return std::allocator<T>::allocate(n, hint);
   }

   countingAllocator() : std::allocator<T>()
   { }

   countingAllocator(const countingAllocator &a) : std::allocator<T>(a)
   { }

   template <class U>
   countingAllocator(const countingAllocator<U> &a) : std::allocator<T>(a)
   { }
   ~countingAllocator()
   { }
};

// Fix for VS Debug build Don`t need for Release
template <>
class countingAllocator<std::_Container_proxy> : public 
  std::allocator<std::_Container_proxy>
{
public:
   template <class U>
   countingAllocator(const countingAllocator<U> &a) : 
     std::allocator<std::_Container_proxy>(a)
   { }
};


std::vector<A, countingAllocator<A>> vecA;
std::vector<B, countingAllocator<B>> vecB;
for (int i{}; i < 1000; i++)
{
    vecA.push_back(A());
    vecB.push_back(B());
}
你可以试试这个:

#include <vector>
#include <iostream>

struct A
{
    int a;
    static int nr;
};

struct B
{
    double b;
    static int nr;
};

int A::nr = 0, B::nr = 0;

int main ()
{
    std::vector<A> vecA;
    std::vector<B> vecB;
    size_t A_capacity = 0, B_capacity = 0;

    for (int i{}; i < 1000; i++)
    {
        vecA.push_back(A());
        if (vecA.capacity () != A_capacity)
        {
            ++A::nr;
            A_capacity = vecA.capacity ();
        }

        vecB.push_back(B());
        if (vecB.capacity () != B_capacity)
        {
            ++B::nr;
            B_capacity = vecB.capacity ();
        }
    }

    std::cout << "A: " << A::nr << ", B: " << B::nr;
}
您可以尝试以下方法:

#include <vector>
#include <iostream>

struct A
{
    int a;
    static int nr;
};

struct B
{
    double b;
    static int nr;
};

int A::nr = 0, B::nr = 0;

int main ()
{
    std::vector<A> vecA;
    std::vector<B> vecB;
    size_t A_capacity = 0, B_capacity = 0;

    for (int i{}; i < 1000; i++)
    {
        vecA.push_back(A());
        if (vecA.capacity () != A_capacity)
        {
            ++A::nr;
            A_capacity = vecA.capacity ();
        }

        vecB.push_back(B());
        if (vecB.capacity () != B_capacity)
        {
            ++B::nr;
            B_capacity = vecB.capacity ();
        }
    }

    std::cout << "A: " << A::nr << ", B: " << B::nr;
}

您的代码从未调用您自己的
new[]
-您希望在什么时候调用它?为什么?我怀疑您正在使用的代码中,重载的
新的
操作符根本不会被调用。您需要知道分配了多少次内存,还是只知道构建了多少次对象?向量不会为单个实例分配内存。为了遵守w由于添加元素的复杂性限制,他们必须在chunck中分配内存,其中每个chunck都比前一个大。我不知道计算向量分配数量的可靠方法。你可以尝试提供自己的to
std::vector
,跟踪分配数量(但在我看来,这似乎有些过分)您的代码从不调用自己的
new[]
-您希望在什么时候调用它?为什么?我怀疑您正在使用的代码中,重载的
新的
操作符根本不会被调用。您需要知道分配了多少次内存,还是只知道构建了多少次对象?向量不会为单个实例分配内存。为了遵守w由于添加元素的复杂性限制,他们必须在chunck中分配内存,其中每个chunck都比前一个大。我不知道计算向量分配数量的可靠方法。你可以尝试提供自己的to
std::vector
,跟踪分配数量(但在我看来,这似乎有些过分)@比亚格,确认我对你的问题的最后理解是否正确。我将尝试改进答案。@比亚格,确认我对你的问题的最后理解是否正确。我将尝试改进答案。