0

This is my function.

    template<class KEY, class T, int (*thash)(const KEY& a)>
HashMap<KEY, T, thash>& HashMap<KEY, T, thash>::operator =(const HashMap<KEY, T, thash>& rhs) {
      if (this == &rhs)
        return *this;

      delete_hash_table(map, bins);
      bins = rhs.bins, used = rhs.used;
      hash = rhs.hash;
      map = this->copy_hash_table(rhs.map, rhs.bins);
      std::cout << *this << std::endl;
      ++mod_count;
      std::cout << "DONEZO" << std::endl;
      return *this;
}

The only other real functions it calls is delete_hash_table, which does not throw the allocation error (because it's deallocating), and copy_hash_table, which I thought may throw the error, but my std::cout prints just fine.

Just in case, here is my copy_hash_table function.

    template<class KEY, class T, int (*thash)(const KEY& a)>
typename HashMap<KEY, T, thash>::LN* HashMap<KEY, T, thash>::copy_list(LN* l) const {
    if (l == nullptr)
        return nullptr;
    return new LN(Entry(l->value.first, l->value.second), copy_list(l->next));
}

template<class KEY, class T, int (*thash)(const KEY& a)>
typename HashMap<KEY, T, thash>::LN** HashMap<KEY, T, thash>::copy_hash_table(LN** ht, int bins) const {
    std::cout << "ASSIGNMENT" << std::endl;
    LN **to_return = new LN*[bins];
    for (int x = 0; x < bins; x++)
        to_return[x] = copy_list(ht[x]);
    return to_return;
}

Donezo gets printed right before I return. I'm not sure what's causing the error.

Error: std::bad_alloc thrown in test body.

Jacob Macallan
  • 959
  • 2
  • 8
  • 29
  • You're sure all of the allocation sizes are larger than zero and smaller than too big? And are you confident the exception throws from there? I'm not sure if you're saying "Donezo" _is_ printed (meaning the function is over and there shouldn't be an exception) or _should_ be printed (meaning it wasn't). – Weak to Enuma Elish Feb 29 '16 at 06:05
  • What does your test body look like? – Jarra McIntyre Feb 29 '16 at 06:06
  • Donezo IS printed and I am able to return. My testbody is gigantic (it's provided by my professor). It basically tests my overloaded operator. – Jacob Macallan Feb 29 '16 at 06:08
  • If you're returning from the function, the function isn't throwing an exception.... Try focusing on any pointer writes. If you corrupt your heap, memory allocation can fail. – Weak to Enuma Elish Feb 29 '16 at 06:11
  • Okay. I'm just confused because the error is definitely not thrown throughout any of my function calls, but IS thrown afterwards, which is during my professor's written test body. – Jacob Macallan Feb 29 '16 at 06:26
  • 2
    Sorry, can't do anything without MCVE or a crystal ball :) But what I *can* tell you is that you should implement your assignment operator with Copy and Swap. It will make things infinitely easier and give 100% exception safety (provided copy and swap are implemented correctly): http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom – Christian Hackl Feb 29 '16 at 07:14
  • This isn't the problem, but don't use `std::endl` unless you need the extra stuff that it does. `'\n'` ends a line. – Pete Becker Feb 29 '16 at 15:52
  • This line `bins = rhs.bins, used = rhs.used;` certainly looks suspicious. It stores the value of `rhs.used` in both `used` **and** `bins`. Change the comma to a semicolon, and put the two assignments on separate lines. – Pete Becker Feb 29 '16 at 15:55
  • @PeteBecker are you sure? I'm seeing bins being assigned to rhs.bins and used being assigned to rhs.used. – Jacob Macallan Feb 29 '16 at 20:27
  • @ChristianHackl Hey, yeah an MCVE would have been nicer. Struggle with those sometimes because I'm not quite sure how much information to give out as the file is humongous. If anything, could you explain why the general reasons to why std::bad_alloc can fail? I know it's when you either run out of memory or the new operator fails; however, I decided to put std::couts' everywhere where I declare a NEW and at maximum only like 5-10 are declared at any given time. – Jacob Macallan Feb 29 '16 at 20:28

0 Answers0