Che*_*eng 5 c++ windows visual-c++
我想知道哪种方法正确?
_tcscpy(tchar_pointer, _tcslen(tchar_pointer), _T("Hello World"));
Run Code Online (Sandbox Code Playgroud)
要么
_tcscpy(tchar_pointer, _tcsclen(tchar_pointer), _T("Hello World"));
Run Code Online (Sandbox Code Playgroud)
要么
_tcscpy(tchar_pointer, ???, _T("Hello World"));
Run Code Online (Sandbox Code Playgroud)
小智 7
假设您正在使用_tcscpy_s和不使用_tcscpy,则第二个参数应该是数组的实际大小,而不是当前包含的字符串的长度.例如:
TCHAR dest[20];
_tcscpy_s(dest, _countof(dest), _T("Hello"));
Run Code Online (Sandbox Code Playgroud)
您甚至可以使用不需要size参数的2参数版本:
_tcscpy_s(dest, _T("Hello"));
Run Code Online (Sandbox Code Playgroud)
如果tchar_pointer实际上是指针而不是数组(如其名称所暗示的那样),则在确定其实际容量时需要非常小心.需要更多的上下文来建议正确的方法,但是使用包含的字符串的长度来计算缓冲区的大小几乎肯定是错误的方法.
tchar指针来自外部,我的一方不知道指针引用的缓冲区有多大
如果是这样,那么这些都不是你想做的.
所有"安全"功能的工作方式是告诉它们目标缓冲区有多大.
你不知道?您无法使用这些功能.
int buffer_size = _tcslen(xxx) * sizeof(TCHAR)
Run Code Online (Sandbox Code Playgroud)
这不行.所有这一切都将保证复制的字符串不会超过缓冲区中已有的字符串.如果缓冲区尚未初始化,则会失败; 如果缓冲区以a开头'\0',则不会复制任何内容; 等等.
| 归档时间: |
|
| 查看次数: |
18394 次 |
| 最近记录: |