简体   繁体   中英

C++ Runtime Error on push_back for a vector of custom objects

In my class, when I try to push any object onto the vector myCache, I get a runtime error. I know I'm initializing the vector properly, and am stumped about why this is happening.

#ifndef CACHE_H
#define CACHE_H

#include <iostream>
#include "cacheblock.h"
using namespace std;

class Cache
{
  public:
   Cache(int rows, int numWords);
   ~Cache();
   CacheBlock getBlock(int index);

  private:
   vector<CacheBlock> *myCache;
};

#endif

and

Cache::Cache(int rows, int numWords)
{
   myCache = new vector<CacheBlock>;
   CacheBlock test(rows, 0, 0);
   myCache->push_back(test);

/*
   for (int i = 1; i < rows; i++)
   {
      myCache->push_back(test);
      cout << "inside loop\n\n";
   }
*/
}

CacheBlock.h:

class CacheBlock
{
  public:
   CacheBlock(int numWords, int newIndex, int tagNum);
   CacheBlock(const CacheBlock &newCacheBlock);
   ~CacheBlock();
   void setData(int numWords, int newIndex, int tagNum);

  private:
   bool valid;
   int index;
   int tag;
   vector<int> *dataWords;
};

Can anyone help?

Presumably there is a working Copy Constructor for CacheBlock ?

EDIT : thanks for posting the additional code.

If the destructor for CacheBlock cleans up the allocated vector<int> *dataWords by deletion, then the copy constructor will need to "deep copy" the vector of dataWords . Without this deep copy, when the CacheBlock is copied, there will be two instances of CacheBlock with the same pointer to the vector<int> . When the first instance is cleaned up, the second one will end up with a stray pointer to the now-deleted copy.

It's worthy of mention, as implied by the comment asking why the vectors<> are being allocated from the heap, that had they not been allocated from the heap, but had been mere member variables, none of these problems would have occurred.

To wit:

#ifndef CACHE_H
#define CACHE_H

#include <iostream>
#include "cacheblock.h"
using namespace std;

class Cache
{
  public:
   Cache(int rows, int numWords);
   // no longer need a destructor, as the auto-generated one by the compiler suffices
   // ~Cache();
   // potential optimization to return by const reference, rather than by copy
   const CacheBlock& getBlock(int index) const;

  private:
   vector<CacheBlock> myCache;
};

#endif

and

Cache::Cache(int rows, int numWords)
{
   // no longer need to construct the vector
   // myCache = new vector<CacheBlock>;
   CacheBlock test(rows, 0, 0);
   myCache->push_back(test);
}

CacheBlock.h:

class CacheBlock
{
  public:
   CacheBlock(int numWords, int newIndex, int tagNum);
   // no longer need a copy constructor
   // CacheBlock(const CacheBlock &newCacheBlock);
   // no longer need a destructor, as the compiler-generated one will suffice
   // ~CacheBlock();
   void setData(int numWords, int newIndex, int tagNum);

  private:
   bool valid;
   int index;
   int tag;
   vector<int> dataWords;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM