可以对空指针使用strcpy_s吗?

dentiscalprum 2015-02-15 04:21:02
char *str1=NULL;
str1=new char[20];
char str[7];
strcpy_s(str1,20,"hello world"); 这个有错误吗
...全文
409 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
CodefansZ 2015-02-15
  • 打赏
  • 举报
回复
[quote=引用 5 楼 kuyucman 的回复:] 那可能就是我这里的网络问题了,在学校的时候也是,Git 也上不了
likfeng 2015-02-15
  • 打赏
  • 举报
回复
strcpy_s, wcscpy_s, _mbscpy_s Visual Studio 2005 Other Versions Copy a string. These are versions of strcpy, wcscpy, _mbscpy with security enhancements as described in Security Enhancements in the CRT. errno_t strcpy_s( char *strDestination, size_t numberOfElements, const char *strSource ); errno_t wcscpy_s( wchar_t *strDestination, size_t numberOfElements, const wchar_t *strSource ); errno_t _mbscpy_s( unsigned char *strDestination, size_t numberOfElements, const unsigned char *strSource ); template <size_t size> errno_t strcpy_s( char (&strDestination)[size], const char *strSource ); // C++ only template <size_t size> errno_t wcscpy_s( wchar_t (&strDestination)[size], const wchar_t *strSource ); // C++ only template <size_t size> errno_t _mbscpy_s( unsigned char (&strDestination)[size], const unsigned char *strSource ); // C++ only Parameters strDestination Location of destination string buffer numberOfElements Size of the destination string buffer. strSource Null-terminated source string buffer. Return Value Zero if successful; an error otherwise. Error Conditions strDestination numberOfElements strSource Return value Contents of strDestination NULL any any EINVAL not modified any any NULL EINVAL not modified any 0, or too small any ERANGE not modified Remarks The strcpy_s function copies the contents in the address of strSource, including the terminating null character, to the location specified by strDestination. The destination string must be large enough to hold the source string, including the terminating null character. The behavior of strcpy_s is undefined if the source and destination strings overlap. wcscpy_s and _mbscpy_s are wide-character and multibyte-character versions of strcpy_s respectively. The arguments and return value of wcscpy_s are wide character strings; those of _mbscpy_s are multibyte character strings. These three functions behave identically otherwise. If strDestination or strSource is a null pointer, or if the destination string is too small, the invalid parameter handler is invoked as described in Parameter Validation. If execution is allowed to continue, these functions return EINVAL and set errno to EINVAL. Upon successful execution, the destination string will always be null terminated. In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads. The debug versions of these functions first fill the buffer with 0xFD. To disable this behavior, use _CrtSetDebugFillThreshold. Generic-Text Routine Mappings TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined _tcscpy_s strcpy_s _mbscpy_s wcscpy_s Requirements Routine Required header Compatibility strcpy_s <string.h> Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 wcscpy_s <string.h> or <wchar.h> Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 _mbscpy_s <mbstring.h> Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 For additional compatibility information, see Compatibility in the Introduction. Example // crt_strcpy_s.cpp // This program uses strcpy_s and strcat_s // and strcat to build a phrase. // #include <string.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> int main( void ) { char string[80]; // using template versions of strcpy_s and strcat_s: strcpy_s( string, "Hello world from " ); strcat_s( string, "strcpy_s " ); strcat_s( string, "and " ); // of course we can supply the size explicitly if we want to: strcat_s( string, _countof(string), "strcat_s!" ); printf( "String = %s\n", string ); } Output String = Hello world from strcpy_s and strcat_s!
东莞某某某 2015-02-15
  • 打赏
  • 举报
回复
引用 4 楼 Benjaminzhou93 的回复:
[quote=引用 3 楼 kuyucman 的回复:] 参考: https://msdn.microsoft.com/en-us/library/td1esda9(VS.80).aspx
我这里上不了你那儿能上吗,怎么破[/quote] 什么意思?链接进不去吗?MSDN没有被墙的阿
CodefansZ 2015-02-15
  • 打赏
  • 举报
回复
引用 3 楼 kuyucman 的回复:
参考: https://msdn.microsoft.com/en-us/library/td1esda9(VS.80).aspx
我这里上不了你那儿能上吗,怎么破
东莞某某某 2015-02-15
  • 打赏
  • 举报
回复
errno_t strcpy_s( char *strDestination, size_t numberOfElements, const char *strSource ); 需要注意的是: 1,strDestination的空间要足够 2,strDestination与strSource重叠的话,行为未定义 如果strDestination=NULL,则该函数会返回EINVAL,表明什么都没做。 参考: https://msdn.microsoft.com/en-us/library/td1esda9(VS.80).aspx
encoderlee 版主 2015-02-15
  • 打赏
  • 举报
回复
char *str1=NULL; str1=new char[20]; str1已经=new char[20]了,已经分配内存空间了,就没问题。 str1已经不为空了,所以不叫“对空指针使用strcpy_s”
tangtangtangbaoli 2015-02-15
  • 打赏
  • 举报
回复
一点没问题,就是这么用,str1已经new了空间了,就是别忘了free

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧