I know that when you use strcpy_s you are supposed to supply the size of the destination string as the second parameter. But if the destination string is a char* then I'm not sure I'm doing it right. I have three examples:
char* dest = new char;
// Example 1
CString strTemp = "Bob";
strcpy_s(dest, strTemp.GetLength() + 1, strTemp);
// Example 2
strcpy_s(dest, strlen("Jose")+1, "Jose");
// Example 3
char* c = new char;
c = "Richard";
strcpy_s(dest, strlen(c) + 1,c);
Is this all correct?
In all three examples you're passing the size of the source string. You're supposed to pass the size of the destination buffer so
strcpy_scan verify that there won't be a buffer overflow.Passing the source string's size defeats that check.
strcpy_salready knows the size of the source string; it can dostrlen(src)just as well as you can. What it can't do is auto-detect the size of the destination buffer. It needs to be told its size.All of your examples should instead look something like the above:
destpoints not at a single char, but a large array of chars.Note: Avoid using C strings in C++ as much as possible. It is far, far better to use
std::string. It'll take care of all of this mess for you. You won't have to deal with memory allocation or worry about buffer overflows.