Problems initializing a character array

Ok, this is for homework on hash tables, but this is simple stuff that I thought I could make from previous classes and I tear my hair off. The professor is not responsive enough, so I decided to try here.

We have a hash table of stock objects. Warehouse objects are created as follows:

stock("IBM", "International Business Machines", 2573, date(date::MAY, 23, 1967))

my constructor looks like this:

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 he refuses to highlight on the line

m_symbol = new char[strlen(symbol)+1];

using std :: bad_alloc. name and symbol declared

char * m_name;  
char * m_symbol;

This is definitely strlen (), which goes astray. And this does not seem to happen every time.

cout << symbol << strlen(symbol); 

returns IBM correctly, then crashes

+3
source share
4

++, std::string , char*?

std::string name;
std::string symbol

setSymbol :

void stock::setSymbol(const char* symbol)  
{
    this->symbol = symbol;
}
+1

,

symbol

new char [strlen (symbol) +1];

strlen , ++ runtime . symbol char* , . , ?

+1

Cygwin, - symbol symbol.

, - - !!! , , , , local/member. ( , - this->.)

0

, . , , , , .

. , . SO'ers

0

Source: https://habr.com/ru/post/1744812/


All Articles