Having issues with initializing character array
Posted
by quandrum
on Stack Overflow
See other posts from Stack Overflow
or by quandrum
Published on 2010-05-10T16:38:11Z
Indexed on
2010/05/10
16:54 UTC
Read the original article
Hit count: 279
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): symbol(NULL), 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 (this->symbol)
delete [] this->symbol;
this->symbol = new char[strlen(symbol)+1];
strcpy(this->symbol,symbol);
}
and it refuses to allocate on the line
this->symbol = new char[strlen(symbol)+1];
with a std::bad_alloc. name and symbol are declared
char * name;
char * symbol;
I feel like this is exactly how I've done it in previous code.I'm sure it's something silly with pointers. Can anyone help?
© Stack Overflow or respective owner