0

I'm quite new to programming and it seems that one of the functions give me this error as soon as it runs over this line of code :

std::string name;

Right after I return *this, it goes through deallocating all of the std::vector that are inside the function, and also those associated with some of the named structures that I use. The 'name' variable itself is stored in another std::vector which is member of the class.

Does this mean that the string is written once and everything that should look like it will all point to the same reference ? Or maybe it is something else, but running the debugger line by line clearly fails on this line. Any help would be appreciated !

NOTE - I have deleted all of the previous code to show a simpler exemple of what I had as trouble :

#include <iostream>
#include <fstream>
using namespace std;

int main () {
    string a = "something";
    ofstream output; output.open("a.dat", ios::out|ios::binary);
    output.write((char*)&a, sizeof(a)); output.close();
    
    ifstream input; input.open("a.dat", ios::in);
    string b; 
    input.seekg(0);
    input.read((char*)&b, sizeof(b)); input.close();
    return 0;
}

So if you run this block of code once, and right before running it a second time you remove the three first lines about the output, you have the error I was facing.

jfo420
  • 11
  • 2
  • 1
    You can't guarantee anything when there's an undefined behavior. As usual, create a [example]. (although it's good that you tried to solve the problem with a debugger) – user202729 Aug 16 '21 at 13:12
  • Besides, normally you would want to use unique_ptr rather than `new` and `delete` – user202729 Aug 16 '21 at 13:13
  • 2
    `input.read((char *)&name, sizeof(name));` (where `input` is a `std::ifstream` and `name` is a `std::string`) inherently gives undefined behaviour. The `read()` function ASSUMES it is reading to an array of bytes. `std::string` manages a memory resource, so (at best) your code overwrites pointers and other members of the string, rather than the data it manages. – Peter Aug 16 '21 at 13:16
  • Unrelated: Try to avoid making _very large_ funtions (like `read_graph_from_file `). Break the problem down in smaller functions that does one thing and does them well. – Ted Lyngmo Aug 16 '21 at 13:17
  • 2
    `p = nullptr; delete p;` is equivalent to `p = nullptr;` I think you need to review the basics of memory management or, better, use standard library collections and smart pointers. Also, you need to read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – molbdnilo Aug 16 '21 at 13:20
  • Have a look at how to make a [mcve]. It's your job to narrow down where the error is, not ours. – super Aug 16 '21 at 13:24
  • Sorry for the extra code. Thanks for all the inputs and suggestions. As I started two months ago I take everything. – jfo420 Aug 16 '21 at 13:44
  • Special mention for @Peter, and many thanks for this hint. I solved this using const char* instead, for writing and reading, then I could construct some local string before pushing it into the class vector. I need to read more about string's memory management but this makes it clear now that I can't screw around as much as native types. Everything works fine now ! :D – jfo420 Aug 16 '21 at 16:02
  • `sizeof(std::string)` is a compile time constant. You can put a very long text in the string but `sizeof(std::string)` will not change. The `std::string` contains a `char*` to where the actual data is stored, so you can neither `write` or `read` it like that. – Ted Lyngmo Aug 16 '21 at 21:40

0 Answers0