[英]The strcpy_s function doesn't work in my code
This function is supposed to copy a char[] into the allocated storage.这个 function 应该将 char[] 复制到分配的存储中。 For some reason the buffer is always too small for this operation.由于某种原因,缓冲区对于此操作总是太小。
str(char* f) {
len = strlen(f);
txt = (char*)malloc(len); //txt is a pointer to a char
strcpy_s(txt, len, f);
}
For some reason the buffer is always too small for this operation.由于某种原因,缓冲区对于此操作总是太小。
You forgot to allocate memory for the null terminator.您忘记为 null 终结器分配 memory。 An empty string requires space for one character (the terminator).一个空字符串需要一个字符(终止符)的空间。 A string of length one requires space for two characters (1 + 1).长度为 1 的字符串需要两个字符 (1 + 1) 的空间。 A string of length len
requires space for len + 1
characters.长度为len
的字符串需要len + 1
字符的空间。
That said:那说:
strdup
instead.在 C 中,请改用strdup
。strlen
, malloc
nor strcpy_s
(nor strdup
).在 C++ 中,不要使用strlen
、 malloc
或strcpy_s
(也不要strdup
)。 I recommend std::string
.我推荐std::string
。make sure you include cstring library确保包含 cstring 库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.