MinGW 中超过 15 个字符的字符串问题

Problem with strings longer than 15 characters in MinGW

问题描述

我对 std::getline() 功能有疑问。我是这样使用的:

std::fstream f; // opened in read in text mode
std::string name;
std::getline(f, name);

对于包含短文本的文件,一切正常,f.e。:

example text

但是当我尝试阅读更长的内容时,f.e.:

example text in file longer than 15

我得到(编译后运行):

delete: invalid pointer 00FD7A48 (0040CD22)
*** Checking for memory corruption: START
*** Checking for memory corruption: 0 FOUND

文件

main.cpp:

#include <string>
#include <cstring>
#include "plik.h"
#include "prowadzacy.h"

using namesapce std;

int main(int argc, char** argv) {
    if (argc == 3 && !strcmp(argv[1], "-f")) {
        Prowadzacy* baza = nullptr;
        string fn(argv[2]);
        czytajZPliku(fn, baza);
    }
}

prowadzacy.h:

#include "przedmiot.h"

struct Prowadzacy {
    std::string imie;
    std::string nazwisko;

    Przedmiot* przedmiot = nullptr;

    Prowadzacy* next = nullptr;
};

przedmiot.h:

struct Przedmiot {
    std::string nazwa;
    enum Rodzaj {
        WYKLAD, CWICZENIA, LABORATORIUM, PROJEKT, SEMINARIUM
    } rodzaj;
    enum DzienTygodnia {
        PONIEDZIALEK, WTOREK, SRODA, CZWARTEK, PIATEK, SOBOTA, NIEDZIELA
    } dzien_tygodnia;
    int godzina;
    int czas_trwania;
    std::string miejsce;

    Przedmiot* next = nullptr;
};

plik.h:

#include <fstream>
#include <iostream>
#include "prowadzacy.h"
#include "przedmiot.h"

bool zapiszDoPliku (const std::string, Prowadzacy*&);
bool czytajZPliku (const std::string, Prowadzacy*&);

plik.cpp:

using namespace std;

bool czytajZPliku (const string fn, Prowadzacy*& baza) {
    fstream f;
    f.open(fn, ios::in);
    if (f) {
        string typ;
        Prowadzacy* p = baza;

        while (getline(f, typ, ' ')) {
            cout << typ << endl;
            if (typ.find("[prowadzacy]") != string::npos) {
                string imie, nazwisko;
                while (p != nullptr)
                    p = p->next;
                getline(f, imie, ',');
                getline(f, nazwisko);
                dodajProwadzacego(imie, nazwisko, baza);
            } else if (typ.find("[przedmiot]") != string::npos) {
                string nazwa;
                getline(f, nazwa);
            }
        }
        f.close();
        return true;
    }
    return false;
}

包含我需要获取的数据的文件:

[prowadzacy] Jan,Nowak
[przedmiot] Informatyka w Technologii

我使用 Makefile 和 MinGW。使用标志编译 -std=c++14 -O3 -g -pedantic-errors

问题更新

我编写了简单的新程序:

#include <string>
#include <iostream>
int main () {
    std::string str = "123456789012345";
    std::cout << str << std::endl;
    return 0;
}

我编译和 运行 这些都没有问题。但是当我将其代码更改为:

#include <string>
#include <iostream>
int main () {
    std::string str = "1234567890123456";
    std::cout << str << std::endl;
    return 0;
}

我得到:

1234567890123456
delete: invalid pointer 00F27F48 (004157B0)
*** Checking for memory corruption: START
*** Checking for memory corruption: 0 FOUND

在 Visual Studio 中一切正常。为什么在 MinGW 中它不起作用?

编译: g++ -std=c++14 -pedantic-errors -Wall -Wextra -O3 -g -pedantic-errors -o main main.cpp

更新:我也在 Linux 上测试过这个。在 Linux 上一切正常。问题仅出现在 Windows.

的 MinGW 中

解决方案

在编译命令中添加 -std=c++14 修复了这个问题。

使用 std::string related std::getline function (from <string> 标准 header) 例如

std::string linstr;
std::getline (std::cin, linstr);

避免使用原始 char* 缓冲区。

还要注意 UTF-8 is used everywhere in 2018 (and you can expect your users to configure their system to use UTF-8; or you should handle other character encodings).

最后,您的程序可能有(并且可能有)一些错误(例如一些 undefined behavior) elsewhere. Read How to debug small programs. Compile with all warnings and debug info (so g++ -Wall -Wextra -g with GCC). Use the gdb debugger and valgrind.

更新

when I change its code to:

#include <string>
#include <iostream>
int main () {
   std::string str = "1234567890123456";
   std::cout << str << std::endl;
   return 0;
}

I get invalid pointer

可能,你的电脑有问题(上面的代码是完全合法的 C++11)。我猜你有 mis-installed MinGW,或者一些错误的设置(例如 PATH 或其他)或配置。获得更多帮助;你需要解释更多。

仅供参考,您最后的代码(当然)在我的 Linux 发行版(Debian/Sid、GCC 8.2)上运行良好。我建议安装和使用一些 Linux 发行版,特别是因为它对开发人员更友好(并提供 valgrind,这对于查找错误 非常 有用)