C++11 在每次迭代中创建类的对象会破坏内存吗

C++11 在每次迭代中创建类的对象会破坏内存吗,c++11,memory-leaks,C++11,Memory Leaks,我有两门课,我们叫它们A和B class A { ....some function definitions.. specialFunc1(int count, B b) { stores data in the class B's object to its own containers and count is an integer which maps data from b to an int using std::map

我有两门课,我们叫它们A和B

class A
{
   ....some function definitions..
   specialFunc1(int count, B b)
   {
        stores data in the class B's object to its own containers
        and count is an integer which maps data from b to an int using
        std::map
   }
};


class B
{
      Has containers which are processing and storing data
      and class A can access these containers because they have public
      access specifier

 };



 int main()
 {

    A a;
    for (int i = 0; i < 10000; i++)
    {
         B b;
         Then b call its methods to store data in desired containers
         a.specialFun1(i,a);
    }
    return 0;
 }
A类
{
…一些函数定义。。
specialFunc1(整数计数,B)
{
将类B对象中的数据存储到它自己的容器中
count是一个整数,它使用
标准::地图
}
};
B类
{
具有处理和存储数据的容器
A类可以访问这些容器,因为它们具有公共
访问说明符
};
int main()
{
A A;
对于(int i=0;i<10000;i++)
{
B B;
然后b调用其方法将数据存储在所需的容器中
a、 特别股1(i,a);
}
返回0;
}
我的代码如上所述,它有两个类,我们称它们为A和B,它们相互作用

类B在主函数的循环中加载一些数据,并对其进行一些处理,然后将其传递给类A,后者将每个迭代的数据映射到std::map

每次迭代都会一次又一次地定义对象b

我的理解是,由于对象b作用域在迭代完成后就在循环中,它应该会耗尽内存,因此新的b定义不应该与堆栈上的内存相混淆

我这样想是对的,还是由于类对象b内的连续分配和解除分配,它可能导致内存损坏

我这样想是对的,还是由于类对象b内的连续分配和解除分配,它可能导致内存损坏

你说得对。在C++中允许连续分配和解除分配,因此一般不会导致内存损坏。


当然,任何行为未定义的代码都可能损坏内存。显示的(不完整)代码没有显示未定义的行为。

如果不查看真实代码,很难回答任何有关内存损坏的问题。伪代码没有提供足够的信息。