Ok, this is for homework about hashtables, but this is the simple stuff I thought I was able to do from earlier classes, and I'm tearing my hair out. The professor is not being responsive enough, so I thought I'd try here.
We have a hashtable of stock objects.The stock objects are created like so:
stock("IBM", "International Business Machines", 2573, date(date::MAY, 23, 1967))
my constructor looks like:
stock::stock(char const * const symbol, char const * const name, int sharePrice, date priceDate): m_symbol(NULL), m_name(NULL), sharePrice(sharePrice), dateOfPrice(priceDate)
{
setSymbol(symbol);
setName(name);
}
and setSymbol looks like this: (setName is indentical):
void stock::setSymbol(const char* symbol)
{
if (m_symbol)
delete [] m_symbol;
m_symbol = new char[strlen(symbol)+1];
strcpy(m_symbol,symbol);
}
and it refuses to allocate on the line
m_symbol = new char[strlen(symbol)+1];
with a std::bad_alloc. name and symbol are declared
char * m_name;
char * m_symbol;
It's definitely strlen() that is going astray. And it doesn't seem to happen every time.
cout << symbol << strlen(symbol);
returns IBM correctly, then crashes