简体   繁体   English

使用const类型变量的for循环中的堆错误?

[英]Heap error in for loop using const type variable?

For just practice, using QT library I am trying to select a folder and list the name of the dicom files in that folder. 对于练习,使用QT库我试图选择一个文件夹并列出该文件夹中的dicom文件的名称。 The following is code for that: 以下是代码:

int main(int argc, char* argv[])
{


    QApplication app(argc, argv);
    QString fileName = QFileDialog::getExistingDirectory();


    if (fileName.isEmpty())
    {
        std::cout << "Got an empty file!\n";
    }
    else
    {
        QStringList nameFilter;
        QDir dir(fileName);
        nameFilter << "*.dcm";
        QFileInfoList list = dir.entryInfoList( nameFilter, QDir::Files );
        int numberOfFiles=list.count();

        for(int i=0;i<numberOfFiles;i++)
        {
            QString filena=list[i].fileName();
            string a=filena.toStdString();
            cout<<a<<endl;

        }
    }
   return 0;
}

Here I have found out that the function toStdString , which is actually std::string QString::toStdString () const , gives Heap error. 在这里我发现函数toStdString ,实际上是std::string QString::toStdString () const ,给出了Heap错误。 I know the replacement to get rid of this error is to use toLocal8Bit().constData() , but I'm curious what's the main reason behind the heap error provided by toStdString. 我知道摆脱这个错误的替代是使用toLocal8Bit().constData() ,但我很好奇是什么是toStdString提供的堆错误背后的主要原因。 Is it because it is const type and my for loop is trying overwrite the const variable everytime? 是因为它是const类型而我的for循环每次尝试覆盖const变量?

Your code looks good - make sure your Qt's dll files are compiled with the same compiler (and with same Debug/Release configuration) you are using. 您的代码看起来很好 - 确保您使用相同的编译器(并使用相同的Debug / Release配置)编译Qt的dll文件。

To answer some of your questions: 回答你的一些问题:

Is it because it is const type and my for loop is trying overwrite the const variable everytime? 是因为它是const类型而我的for循环每次尝试覆盖const变量?

No, your for loop is not trying to overwrite const variable. 不,你的for循环不是试图覆盖const变量。 Const variable is on the right side of the assingment operator, so your for loop is reading from const variable, and it doesn't overwritting anything (your a variable is local variable visible inside for loop's block, so a is different in each pass of the loop). const变量位于assingment运算符的右侧,因此你的for循环是从const变量读取的 ,并且它不会覆盖任何东西(你a变量是局部变量,在循环块中可见,所以a在每次传递中都是不同的循环)。

Even if you try to overwrite const variable, that would not compile - trying to change const will break in compile-time, and not in runtime. 即使您尝试覆盖const变量,也不会编译 - 尝试更改const将在编译时中断,而不是在运行时中断。

  1. This code works for me. 这段代码适合我。
  2. You'd better not convert strings to std::string and use std::cout , but rather use QTextStream for output: 你最好不要将字符串转换为std::string并使用std::cout ,而是使用QTextStream进行输出:

     QTextStream out(stdout); //... QString filena=list[i].fileName(); out << filena << endl; 

My money would be on mixing Debug versions of the Qt Framework with Release versions of your compiled program. 我的钱是将Qt Framework的Debug版本与已编译程序的Release版本混合。

Especially under Windows a different heap manager is used in debug and release builds. 特别是在Windows下,在调试和发布版本中使用了不同的堆管理器。

Try recompiling in debug mode and seeing what happens. 尝试在调试模式下重新编译,看看会发生什么。

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

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