title: C++: terminate called after throwing an instance of 'std::bad_alloc'
body: <p>I am implementing a doubly linked list class of sorts which stores 'buckets' (the nodes), which each contain a predefined number of characters. Each bucket stores a pointer to the next and previous bucket, and the list class (BucketString) stores a pointer to the head Bucket. I am compiling using g++ which throws the error</p>
  
  <pre><code>terminate called after throwing an instance of 'std::bad_alloc'
    what(): std::bad_alloc
  make: *** [run] Aborted (core dumped)
  </code></pre>
  
  <p>whenever I run the code and add a string of characters to the list, using the following add method, which is contained within my bucket class, and is called from the list class's own methods whenever needed.</p>
  
  <p>Code:</p>
  
  <pre><code>std::size_t bucketSizeB;
  int filled;
  char* str;
  Bucket* next;
  Bucket* prev;
  
  Bucket::Bucket() : bucketSizeB(7), str(new char[7]), next(NULL), prev(NULL), filled(0)
  {}
  
  Bucket::Bucket(std::size_t bucketSizeB_) : bucketSizeB(bucketSizeB_), str(new char[bucketSizeB]), next(NULL), prev (NULL), filled(0)
  {}
  
  Bucket::Bucket(const Bucket&amp; rhs) : bucketSizeB(rhs.bucketSizeB), next(rhs.next), prev(rhs.prev), filled(rhs.filled)
  {
      for (int i = 0 ; i &lt; (int) bucketSizeB ; i++)
      {
          str[i] = rhs.str[i];
      }
  }
  
  void Bucket::add(std::string line)
  {
  
      int diff = bucketSizeB - filled;    //if the bucket is already partially filled
  
  
      std::string tmp = line.substr(0, diff);
  
      for (std::size_t i = 0 ; i &lt; tmp.length() ; i++)
      {
  
          str[filled] = line[i];
          ++filled;
      }
  
      if (line.length() &gt; bucketSizeB)
      {
  
          next = new Bucket(bucketSizeB);
  
          next-&gt;prev = this;
          next-&gt;add(line.substr(diff, line.length()-diff));
      }
  }
  Bucket::~Bucket()
  {
      if (prev)
      {
          if (next)
          {
              prev-&gt;next = next;
          }
          else
          {
              prev-&gt;next = NULL;
          }
      }
      if (next)
      {
          if (prev)
          {
              next-&gt;prev = prev;
          }
          else
          {
              next-&gt;prev = NULL;
          }
      }
      delete [] Bucket::str;
  }
  </code></pre>
  
  <p>When the error is thrown, the add method is being called from the 'list' class member method append, which works as follows:</p>
  
  <pre><code>void BucketString::append (std::string&amp; line)
  {
      length += line.length();    //Just a way to store the length of the string stored in this BucketString object
  
      if (!head)   //If the head node pointer is currently null, create a new head pointer
      {
  
          head = new Bucket(bucketSize);
      }
  
      Bucket* tmp = head;
  
      while (tmp-&gt;next)   //Finds the tail node
      {
          tmp = tmp-&gt;next;
      }
      tmp-&gt;add(line);   //Calls the Bucket add function on the tail node
  }
  </code></pre>
  
  <p>The header file for the bucket class is:</p>
  
  <pre><code>#include &lt;cstddef&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  #ifndef BUCKET_H_
  #define BUCKET_H_
  
  namespace RBNWES001
  {
  class Bucket
  {
  
      public:
          //Special members and overloaded constructor
          Bucket(void);
          Bucket(std::size_t);
          Bucket(const Bucket&amp;);
          ~Bucket();
          //Copy Assignment not included because it's not needed, I'm the only one who is gonna use this code! :)
  
          //Add method
          void add(std::string);
  
          int filled;
          char* str;
          Bucket* next;
          Bucket* prev;
          std::size_t bucketSizeB;
  };
  }
  
  #endif
  </code></pre>