I've browsed topics on this error message: "free(): double free detected in tcache 2" but none covers my case (at least not in the direct way).
The program executes and returns correct values. However, I receive
free(): double free detected in tcache 2
after its run.
If I remove lines corresponding to char array, marked as //ERROR
there's no such issue. Could someone help me to understand what's going on there?
The code:
#include <cstring>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#pragma pack(push, r1, 1)
struct Header2 {
char id[15]; // ERROR
std::vector<int> length = std::vector<int>(2);
int count;
};
#pragma pack(pop, r1)
int main() {
Header2 h;
strcpy(h.id, "xdddd"); // ERROR
h.length = {3, 5};
h.count = 42;
std::fstream fh2;
fh2.open("test.txt", std::fstream::out | std::fstream::binary);
fh2.write((char*)&h, sizeof(Header2));
fh2.close();
Header2 h3;
fh2.open("test.txt", std::fstream::in | std::fstream::binary);
fh2.read((char *) &h3, sizeof(Header2));
fh2.close();
if(!fh2.good()) {
std::cout << "Error occurred at reading time!" << std::endl;
return 1;
}
std::vector<int> out = h3.length;
std::cout << h3.id << " " << std::endl; // ERROR
std::cout << out[0] << "," <<out[1] << " " << h3.count << std::endl;
std::cout << "End of program" << std::endl;
return 0;
}
In the code, it is important that the sizes of struct variables are fixed, because different programs would read output files.
The problem investigated with TIO Run gives additional message: /srv/wrappers/cpp-gcc: line 5: 15547 Aborted (core dumped) ./.bin.tio "$@" < .input.tio)