dimanche 23 avril 2017

Does creating an object of a class in every iteration corrupts memory

I have two classes let's call them A and 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;
 }

My code is as described above, it has two classes let's call them A and B which are interacting with each other.

Class B loads in some data inside the loop in the main function does some processing on it and then passes it to class A which maps each iteration's data to a std::map.

And every successive iteration defines object b again and again.

My understanding is, since object b scope is just within the loop as soon as the iteration completes it should go out of memory and hence a new definition of b should not mess up with the memory on the stack.

Am I right in thinking this or it can potentially cause memory corruption due to successive allocation and deallocation inside the class object b.

Aucun commentaire:

Enregistrer un commentaire