简体   繁体   English

Qt程序崩溃与另一个编译器

[英]Qt Program crashes with another compiler

Could anyone tell me what's wrong with this part of my code? 谁能告诉我这部分代码有什么问题吗? It crashes during execution. 执行期间崩溃。

void MainWindow::on_pushButton_clicked()
{
char * cstr = new char [ui->lineEdit->text().length()];
       string costam;
       costam=ui->lineEdit->text().toStdString();
       strcpy(cstr, costam.c_str()); <<<----TROUBLE LINE
       int z;
       z=costam.length();
       for(int n=0;n<z;n++){
            string wynik;
            wynik=konwersja(cstr[n]);
            mors(wynik);
            Sleep(300);
           }
   delete[] cstr;
   }

When I try to compile it with MinGW in Qt 5.0.1 everything is OK but with MSVC2010 in Qt 4.8.1 there is a warning: 当我尝试在Qt 5.0.1中使用MinGW进行编译时,一切正常,但是在Qt 4.8.1中使用MSVC2010进行编译时,出现警告:

warning C4996: 'strcpy': This function or variable may be unsafe. 警告C4996:'strcpy':此函数或变量可能不安全。 Consider using strcpy_s instead. 考虑改用strcpy_s。 To disable deprecation, use _CRT_SECURE_NO_WARNINGS. 要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。 See online help for details. 详细信息请参见在线帮助。

Your question is wrong. 您的问题是错的。 This code compiles and there's nothing about QT here. 这段代码可以编译,这里没有关于QT的内容。

strcpy is deprecated because it's unsafe. 不建议使用strcpy ,因为它不安全。 Alternative is strcpy_s : 替代方法是strcpy_s

strcpy_s( cstr, ui->lineEdit->text().length() + 1, costam.c_str() );

Note that you should allocate ui->lineEdit->text().length() + 1 , not ui->lineEdit->text().length() . 请注意,您应该分配ui->lineEdit->text().length() + 1 ,而不是ui->lineEdit->text().length() That's the reason of crash, I guess. 我猜这就是崩溃的原因。

BTW I see no reason to use cstr array in your code at all. 顺便说一句,我认为根本没有理由在您的代码中使用cstr数组。 As example: 例如:

void MainWindow::on_pushButton_clicked() {
    string costam;
    costam = ui->lineEdit->text().toStdString();
    for( size_t n = 0; n < costam.length(); n++ ) {
        string wynik;
        wynik = konwersja( costam[ n ] );
        mors( wynik );
        Sleep( 300 );
        }
    }

Can you change this line strcpy(cstr, costam.c_str()); 您可以更改此行吗? strcpy(cstr, costam.c_str()); with strcpy_s(cstr, costam.c_str()); strcpy_s(cstr, costam.c_str()); try compiling again? 尝试再次编译?

Also it shouldn't prevent compiling, MSVC2010 is just warning about an unsafe usage. 同样,它也不应该阻止编译,MSVC2010只是警告使用不安全。 You can also lower the warning level of MSVC2010 . 您还可以降低MSVC2010的警告级别。

Because people are not very aware of security when programming C++, and Windows gets a bad rap because of it, Visual Studio "deprecated" several functions which are common causes of buffer overflows. 由于人们在编程C ++时并不十分了解安全性,并且Windows对此表示不满,因此Visual Studio“弃用了”几个导致缓冲区溢出的常见功能。 In this case you should be fine, and can probably just disable the warning by defining _CRT_SECURE_NO_WARNINGS. 在这种情况下,您应该没问题,并且可以通过定义_CRT_SECURE_NO_WARNINGS禁用警告。 You may also find this issue with posix functions, too, in which case you would be able to disable those warnings with a separate #define. posix函数也可能会出现此问题,在这种情况下,您可以使用单独的#define禁用这些警告。

You have two problems here. 您在这里有两个问题。

The crash is because strcpy will write length + 1 characters into the destination buffer, but your buffer is only of size length . 崩溃是因为strcpy会将length + 1字符写入目标缓冲区,但是您的缓冲区只有size length The +1 is for the null termination character, which is not included in length . +1是空终止符,不包含在length

The warning is because Microsoft believes its too easy to make mistakes using strcpy and discourages its use. 警告是因为Microsoft认为使用strcpy容易出错并且不鼓励使用它。 As Joel mentioned, you could enable a define to prevent that warning. 如Joel所述,您可以启用一个定义来防止该警告。 I would not recommend semihyagcioglu and Microsoft's suggestion of using strcpy_s, as it's not a portable solution. 我不建议使用semihyagcioglu和Microsoft建议使用strcpy_s,因为它不是便携式解决方案。

I'd also like to note that while fixing those things will make your code compile and run without error, there are other questions. 我还想指出,修复这些问题将使您的代码得以编译和运行而不会出错,但还有其他问题。 Like: why do you need the cstr variable in the first place? 就像:为什么首先需要cstr变量? cstr[n] can probably be replaced with costam.data()[n]. cstr [n]可能可以替换为costam.data()[n]。 Then the cstr variable won't need to exist at all. 然后,cstr变量根本不需要存在。 You won't need the new, delete or strcpy. 您将不需要new,delete或strcpy。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM