c++ - How to avoid strcpy_s buffer too small -
i need re-create std::string data
char array. length of string variable length of char array fixed.
const int size = 5; char name[size]; std::string info = "1234567890"; strcpy_s(name, 5, data.c_str()); //causes buffer little assertion strcpy_s(name, 11, data.c_str());//copies fine (length of info plus null) strcpy_s(name, sizeof(data), data.c_str()); // copies fine
how can re-create length of array safely each time? without getting assert , without causing buffer on run.
should each time?
std::string tocopy = data.substr(0,size-1); strcpy_s(name, tocopy.c_str());
use strncpy_s _truncate
for instance:
strncpy_s(name, data.c_str(), _truncate);
will re-create much can fill name
buffer while still taking null termination business relationship (unlike traditional strncpy).
c++ c char
No comments:
Post a Comment