crash - adding to a map and set STL - c++

1

I have a cd , dvd. book media program. Currently, i am trying to add book information to a set. I am getting a nasty crash.

error: Unhandled exception at 0x00449d76 in a04xc.exe: 0xC0000005: Access violation reading location 0x00000014.

So, obviously its trying to access memory its not suppose to? just not sure why or what i need to do to add information?

Its coming from this line in this function...

const Item* Library::addBook(const string& title, const string& author, const int nPages)
{

Book* item = new Book(title,author,nPages);
allBooks.insert(item); // add to set of all books
allBooksByAuthor[author]->insert(item);  // causing error..
return item;

}

here is the driver..

// add items to library
cout << ">>> adding items to library:\n\n";
item = library->addBook("The Curious Incident of the Dog in the Night-Time", "Mark Haddon", 240);
if (item != NULL) {
    library->addKeywordForItem(item, "autism");
    library->addKeywordForItem(item, "Asperger's Syndrome");
    library->printItem(cout, item);
    }

here is part of the library cpp where i am having problems with addbooks function

#include "Library.h"
#include "book.h"



ItemSet allBooks;                // for my sets defined in the Items cpp
ItemSetMap allBooksByAuthor;


void Library::addKeywordForItem(const Item* item, const string& keyword)
{

//item->addKeyword(keyword);

}

const ItemSet* Library::itemsForKeyword(const string& keyword) const
{
return NULL;
}

void Library::printItem(ostream& out, const Item* const item) const
{
}

// book-related functions

const Item* Library::addBook(const string& title, const string& author, const int nPages)
{

Book* item = new Book(title,author,nPages);
allBooks.insert(item); // add to set of all books
allBooksByAuthor[author]->insert(item);  // add to set of books by this author
return item;

}

here is the items class

#pragma once

#include <ostream>
#include <map>
#include <set>
#include <string>
#include "Item.h"

using namespace std;

typedef set<Item*>              ItemSet;
typedef map<string,Item*>       ItemMap;
typedef map<string,ItemSet*>    ItemSetMap;

class Library
{

public:
// general functions

void addKeywordForItem(const Item* const item, const string& keyword);
const ItemSet* itemsForKeyword(const string& keyword) const;
void printItem(ostream& out, const Item* const item) const;

// book-related functions

const Item* addBook(const string& title, const string& author, int const nPages);
const ItemSet* booksByAuthor(const string& author) const;
const ItemSet* books() const;

// music-related functions

const Item* addMusicCD(const string& title, const string& band, const int nSongs);
void addBandMember(const Item* const musicCD, const string& member);
const ItemSet* musicByBand(const string& band) const;
const ItemSet* musicByMusician(const string& musician) const;
const ItemSet* musicCDs() const;

// movie-related functions

const Item* addMovieDVD(const string& title, const string& director, const int nScenes);
void addCastMember(const Item* const movie, const string& member);
const ItemSet* moviesByDirector(const string& director) const;
const ItemSet* moviesByActor(const string& actor) const;
const ItemSet* movies() const;
};

here is book.h

#ifndef BOOK_H
#define BOOK_H

#pragma once
#include "item.h"

using namespace std;

class Book : public Item
{
public:
Book(const string& title, const string& author, const int nPages);
~Book();
const int getPages() const;
const string getAuthor() const;
virtual void print(ostream& out) const;

private:

int numPages;
string Author;

};
ostream& operator<<(ostream& out, const Book* book);
#endif

I am just learning STL and i am having a hard time learning whats going on here. i understand set. I am just not sure in my situation map is for? is it adding item to author? and them adding them both to ItemSetMap?

typedef set<Item*>                ItemSet;
typedef map<string,Item*>         ItemMap;
typedef map<string,ItemSet*>      ItemSetMap;

so, how can i fix this crash? am i not adding author to item correctly?

Thank You..

c++
asked on Stack Overflow Mar 2, 2010 by (unknown user) • edited Mar 2, 2010 by (unknown user)

3 Answers

5

I believe typedef map<string,ItemSet*> ItemSetMap; should be typedef map<string, ItemSet> ItemSetMap;

allBooksByAuthor[author] currently returns an ItemSet pointer, but you really want to be dereferencing an ItemSet, which you would do by making the typedef change mentioned above, and changing your erroring line to be:

allBooksByAuthor[author].insert(item);

answered on Stack Overflow Mar 2, 2010 by JonM
1

In Library::addBook(): When you access allBooksByAuthor[author], check that author is in fact in the map. operator[]() of std::map<K,V> returns a reference to the value of the key you're searching, but if the key is not in the map it creates an entry for that key and then returns a reference for the key's value.

In your code, if the key author doesn't exist in the map then the map will create a new entry for that key with value (of type ItemSet*) default-initialized. Now, when you receive a reference to an uninitialized pointer and then attempt to dereferece it the result behaviour is undefined.

answered on Stack Overflow Mar 2, 2010 by wilhelmtell
1

The correct answer is to change the map to contain set objects, rather than pointers, as JonM says.

However, if you are constrained to store pointers, then you will need to manually create a new set when it's needed. For a new author, using allBooksByAuthor[author] will insert a new entry with a null pointer, which you mustn't dereference. In that case, you'll need to create a new set and update the pointer in the map. You could also look at allBooksByAuthor.find(author), which has different behaviour for a new author.

You'll then need to make sure that the sets are deleted when they are removed from the map, and any remaining ones are deleted when the map is deleted, otherwise the memory allocated for them will be lost for ever. This kind of resource leak is one reason why you should never deal with raw pointers like this; storing objects in the map would completely take care of memory management for you.

Since this is homework, I won't give you any code - this should be enough to get you started.

answered on Stack Overflow Mar 2, 2010 by Mike Seymour • edited Mar 2, 2010 by Mike Seymour

User contributions licensed under CC BY-SA 3.0