Stack-based buffer overrun attempting to create an object of class

429 Views Asked by At

Prehistory: I am creating template class(HashMap< KeyType, ValueType >), which should implement abstraction of map using hash tables. To solve collisions in hash tables method of chains is used. I get error when I attempt to initialize HashMap object in main.cpp using constructor HashMap().

Error: "stack cookie instrumentation code detected a stack-based buffer overrun."

So, the code:

Part of private section(only all member variables and method createBuckets(), which is used in constructor HashMap())

private:

/* Constant definitions */

   static const int INITIAL_BUCKET_COUNT = 101;
   static const int MAX_LOAD_PERCENTAGE = 70;

/* Type definition for cells in the bucket chain */

   struct Cell {
      KeyType key;
      ValueType value;
      Cell *next;
   };

/* Instance variables */

   vector<Cell *> buckets;
   int nBuckets;
   int numEntries;

/* Private methods */

/*
 * Private method: createBuckets
 * Usage: createBuckets(nBuckets);
 * -------------------------------
 * Sets up the vector of buckets to have nBuckets entries, each NULL.  If
 * asked to make empty vector, makes one bucket just to simplify handling
 * elsewhere.
 */

void createBuckets(int new_nBuckets) {
    if (new_nBuckets == 0) nBuckets = 1;
    buckets = vector<Cell *>(new_nBuckets, NULL);
    this->nBuckets = new_nBuckets;
    numEntries = 0; 
}
template <typename KeyType, typename ValueType>
HashMap<KeyType, ValueType>::HashMap() {
    createBuckets(INITIAL_BUCKET_COUNT);
}

The most strange that the error happens rarely and when it does not happen, class works successufully. If I recompile the programm, there is no error and it do not happen until the next recompling. If it can matter, I use Visual Studia 2017 for writing this programm.

I suppose that problem is caused by creating of vector in method createBuckets, but I can not see any problem with this.

0

There are 0 best solutions below