strcpy_s buffer L buffer太小&& 0

时间:2021-09-28 20:22:07

i have a problem.

我有个问题。

i tried to proceede the following steps:

我试图执行以下步骤:

    char * str;
    char * s="Hello";
    int len = std::strlen(s);
        str=new char[len +10];
    strcpy_s(str,sizeof(str) ,s);

But the programm displays the error, which i write in the title.

但是程序显示错误,我在标题中写道。

If i replace the sizeof(str) with a number, for example 256, a pop up window appear with the message project.exe has triggered a breakpoint.

如果我用一个数字(例如256)替换sizeof(str),会出现一个弹出窗口,其中包含project.exe已触发断点的消息。

How can i solve this errors with strcpy_s?? Thank you!

如何用strcpy_s解决这个错误?谢谢!

2 个解决方案

#1


0  

sizeof(str) gives the size of char*, which is not the length of the allocated array. To fix this, you must give the allocated length. In this case this would be

sizeof(str)给出char *的大小,它不是分配的数组的长度。要解决此问题,您必须提供分配的长度。在这种情况下,这将是

strcpy_s(str, len + 10, s);

#2


0  

The sizeof(str) returns the size of char* which is (probably) either 4 or 8 (bytes) depending on the implementation. In your case it appears to be 4 which is not enough to hold the characters that make up the "Hello" literal plus the null terminating character. Since there is not enough space the strcpy_s function invokes undefined behavior. You need at least 6 bytes which is the number of characters + 1 byte for a null character. Also instead of str=new char[len + 10]; you probably meant str = new char[len + 1]; to accommodate for the \0 character. Hence your code should be:

sizeof(str)返回char *的大小,取决于实现,它可能(可能)为4或8(字节)。在你的情况下,它似乎是4,这不足以容纳构成“Hello”文字的字符加上空终止字符。由于没有足够的空间,strcpy_s函数会调用未定义的行为。您需要至少6个字节,即空字符的字符数+ 1个字节。而不是str = new char [len + 10];你可能意味着str = new char [len + 1];适应\ 0字符。因此,您的代码应该是:

#include <iostream>

int main(){
    char* str;
    char* s = "Hello";
    int len = std::strlen(s);
    str = new char[len + 1];
    strcpy_s(str, len + 1, s);
}

That being said prefer std::string to C-style character array.

据说更喜欢std :: string到C风格的字符数组。

#1


0  

sizeof(str) gives the size of char*, which is not the length of the allocated array. To fix this, you must give the allocated length. In this case this would be

sizeof(str)给出char *的大小,它不是分配的数组的长度。要解决此问题,您必须提供分配的长度。在这种情况下,这将是

strcpy_s(str, len + 10, s);

#2


0  

The sizeof(str) returns the size of char* which is (probably) either 4 or 8 (bytes) depending on the implementation. In your case it appears to be 4 which is not enough to hold the characters that make up the "Hello" literal plus the null terminating character. Since there is not enough space the strcpy_s function invokes undefined behavior. You need at least 6 bytes which is the number of characters + 1 byte for a null character. Also instead of str=new char[len + 10]; you probably meant str = new char[len + 1]; to accommodate for the \0 character. Hence your code should be:

sizeof(str)返回char *的大小,取决于实现,它可能(可能)为4或8(字节)。在你的情况下,它似乎是4,这不足以容纳构成“Hello”文字的字符加上空终止字符。由于没有足够的空间,strcpy_s函数会调用未定义的行为。您需要至少6个字节,即空字符的字符数+ 1个字节。而不是str = new char [len + 10];你可能意味着str = new char [len + 1];适应\ 0字符。因此,您的代码应该是:

#include <iostream>

int main(){
    char* str;
    char* s = "Hello";
    int len = std::strlen(s);
    str = new char[len + 1];
    strcpy_s(str, len + 1, s);
}

That being said prefer std::string to C-style character array.

据说更喜欢std :: string到C风格的字符数组。