我收到了一个警告:
warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead.
To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
F:\vuStudio\VC\include\string.h(188) : see declaration of 'strncpy'
我在stackoverflow.com上读到,strcpy
不安全,应该使用strncpy
代替。但是现在为什么我会收到警告说strncpy
不安全?
我调用的方式如下:
strncpy(this->title, title.c_str(), sizeof(this->title));
title.c_str()
的长度超过了sizeof(this->title)
,那么this->title
数组将不会被空终止,并且如果您稍后尝试使用它,可能会导致随机崩溃。请使用strncpy(this->title, title.c_str(), sizeof(this->title)-1); this->title[sizeof(this->title)-1] = '\0';
来解决这个问题。 - pqnet