I wrote the following C++ constructor, and I get an error - BUFFER too small on strcpy_s
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Trace::Trace(constchar *str)
{
if (str)
{
int len = strlen(str);
this->m_name = newchar[len+1]; // asking for 'len+1' memory elements of char
strcpy_s(m_name, len, str); // **** I get here an error "BUFFER TOO SMALL" ****
cout << "start of " << m_name << endl;
}
else
{
m_name = newchar[1];
m_name[0] = '\0';
}
}
Use len + 1 as size arguments for strcpy_s() or, even better, don't use it at all :)
1 2 3 4 5 6 7
if (str)
{
int len = strlen(str);
this->m_name = newchar[len+1]; // asking for 'len+1' memory elements of char
strcpy_s(m_name, len + 1, str); // **** I get here an error "BUFFER TOO SMALL" ****
cout << "start of " << m_name << endl;
}