title: Allocating memory for class in C++
body: <p>This is my class:</p>
  
  <pre><code>using namespace std;
  
  Class Book {
      public:
          Book();
          Book(vector&lt;string&gt;*, string, int);
          Book(const Book&amp;);
          ~Book();
          Book&amp; operator=(const Book&amp;);
          void update(vector&lt;string&gt;*);
          void update(string); 
          void update(int); 
          int getYear() const{
              return year;
          };
          string getTitle() const{
              return title;
          };
          bool operator==(const Book&amp;);
          bool operator!=(const Book&amp;);
          friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Book&amp;);
          void getAuthors();
      private:
          vector&lt;string&gt;* authors;
          string title;
          int year;
  };
  
  #endif  /* BOOK_H */
  </code></pre>
  
  <p>Here is the source for it:</p>
  
  <pre><code>#include "Book.h"
  using namespace std;
  
  Book::Book():year(0), title(NULL), authors(NULL){}
  Book::Book(vector&lt;string&gt;* bookauthors,string booktitle, int bookyear ){
      authors = bookauthors;
      title = booktitle;
      year = bookyear;
  }
  
  Book::Book(const Book&amp; aBook){
      authors = aBook.authors;
      title = aBook.title;
      year = aBook.year;
  }
  
  Book::~Book(){
      delete authors;
      delete &amp;title;
      delete &amp;year;
  }
  
  bool Book::operator==(const Book &amp;aBook){
      if(getYear() == aBook.getYear() &amp;&amp; getTitle() == aBook.getTitle())
          return true;
      else return false;
  }
  
  bool Book::operator != (const Book &amp;aBook){
      if(getYear() != aBook.getYear() &amp;&amp; getTitle() != aBook.getTitle())
          return true;
      else return false;
  }
  
  Book&amp; Book::operator =(const Book&amp; rhs){
      if(this != &amp;rhs){
          authors = rhs.authors;
          title = rhs.title;
          year = rhs.year;
      }
      return *this;
  }
  
  void Book::update(int newyear){
      year = newyear;
  }
  
  void Book::update(string newtitle){
      title = newtitle;    
  }
  
  void Book::update(vector&lt;string&gt;* newauthors){
      authors = newauthors;
  }
  
  std::ostream&amp; operator &lt;&lt;(std::ostream&amp; os, const Book&amp; b){
      os&lt;&lt;b.getTitle()&lt;&lt;", "&lt;&lt;b.getYear();
      return os;
  }
  </code></pre>
  
  <p>Here is the main file where it runs:</p>
  
  <pre><code>    #include "Book.h"
  #include &lt;iostream&gt;
  #include &lt;limits.h&gt;
  //This is the test funcion posted on the class website
  using namespace std;
  
  int main(){
  
    //testing constructor
    vector&lt;string&gt; authors;
    authors.push_back("Ritchie");
    authors.push_back("Kernighan");
    Book a(&amp;authors, "C", 1990);
    authors.push_back("Whatever");
    cout &lt;&lt; "Book a is: " &lt;&lt; a &lt;&lt; endl;
    cout &lt;&lt; "Expected: (C, 1990, Ritchie &amp; Kernighan)" &lt;&lt; endl;
  
    //testing copy constructor
    Book b(a);
    a.update(&amp;authors);
    cout &lt;&lt; "Book b is: " &lt;&lt; b &lt;&lt; endl;
    cout &lt;&lt; "Expected: (C, 1990, Ritchie &amp; Kernighan)" &lt;&lt; endl;
  
    //testing constructor
    vector&lt;string&gt; authors2;
    authors2.push_back("Crockford");
    Book c(&amp;authors2, "JavaScript", 2008);
    cout &lt;&lt; "Book c is: " &lt;&lt; c &lt;&lt; endl;
    cout &lt;&lt; "Expected: (JavaScipt, 2008, Crockford)" &lt;&lt; endl;
  
    //testing assignment operator
    authors2.push_back("whatever");
    a=c;
    cout &lt;&lt; "Book a is changed to: " &lt;&lt; a &lt;&lt; endl;
    cout &lt;&lt; "Expected: (JavaScipt, 2008, Crockford)" &lt;&lt; endl;
  
    for(int i=0; i &lt; 200000000; i++)
      b=c;
    cout &lt;&lt; "Book b is changed to: " &lt;&lt; b &lt;&lt; endl;
    cout &lt;&lt; "Expected: (JavaScipt, 2008, Crockford)" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>I keep getting this when I run it: </p>
  
  <blockquote>
    <p>bookclass(58316) malloc: <strong>* error for object 0x7fff522d78b0: pointer being freed was not   allocated*</strong> set a breakpoint in malloc_error_break to debug</p>
  </blockquote>
  
  <p>I'm new to C++ so I'm not sure how to allocate the memory. I tried using <code>malloc</code> and it didn't work.</p>