当使用 memcopy 将数据从 const 字符串复制到数组 -(-> 这是结构体的成员)时,编译器会因目标对象中的缓冲区溢出而抛出错误
感谢您的学习。
#define LEN 9
typedef struct buff_ {
....
..
char bowl[LEN];
}buff;
buff *dest= NULL;
dest= malloc......();// This is allocated properly
'
'
'
memcpy(dest->bowl,"y",LEN); //compiler throwing error here:buffer overflow
当我将字符串复制到 LEN 大于 src 的缓冲区(dest)时,不应该出现任何错误。
最佳答案
尝试使用,这将限制为 LEN 或字符串的实际长度,以较小者为准:
strncpy(dest->bowl,"y",LEN);
关于使用 memcpy 的 dest 对象中的缓冲区溢出的编译器警告/错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55638577/