I have made the vector that I read the file data into const to avoid the vector or its elements being changed. And if I uncomment either of the two commented lined, I get compile time errors, just as expected due to the vector being const.
Now I know that vector has two data() methods, one that's const and one that's non-const, and it seems to me that the decision of which to use depends on the vector itself being const or not.
But then why can I read into the vector using the data() method as I do with the istream? Since the vector is constant, both calls to data() should be using the const method. So how come the one in the istream.read() isn't but instead using the non-const version?
test.data() is a std::string *. Line 14 overwrites the first sizeof(std::vector<std::string>) bytes (not the first sizeof(std::string) bytes) of test[0] with whatever was on the file. Since std::string contains pointer members, this effectively invalidates the object and leaks memory by making it impossible to recover the internal array used by std::string. The behavior of the program after this point is undefined.
This is a correct way to read a binary file into an std::vector:
1 2 3 4 5
std::ifstream file(path, std::ios::binary|std::ios::ate);
//TODO: check that file is open.
std::vector<char> buffer(file.tellg());
file.seekg(0);
file.read(&buffer[0], buffer.size());
There's no point in answering your questions because they exist in the context of a program that has undefined behavior.
Well, I just used string because I needed a vector with some class, and string was the first I thought of. I never considered the problems with pointers. So therefore I made my own test class and changed my code to use that.
1 2 3 4 5 6 7 8 9 10 11
#pragma once
class PointerlessClass
{
private:
char c;
public:
PointerlessClass();
PointerlessClass(char ch);
~PointerlessClass();
char get() const;
};
This shows the exact same behaviour as the code above though. I get exactly the data back that I wrote to the file, as can be seen by the couts. And I still get errors if I remove the comments. And in this case test.data() would return PointerlessData *
So my question still stands as to why I can write t.data() in the istream but not in my own code.
Now you're overflowing the vector's internal buffer. The size parameter to write() and read() must be <= test.size().
PointerlessClass is not a POD type, anyway, so using it in conjunction with read() and write() is also unsafe (although compilers will generally produce correct code anyway).
So my question still stands as to why I can write t.data() in the istream but not in my own code.
read() can modify the contents of the vector because you're bypassing the protections of const by explicitly casting the return value of data() to a pointer to non-const type. This also has undefined behavior. A clever enough compiler could notice that the contents of the vector can't legally change and would replace line 21 with cout << "Here:U"<< endl; Existing compilers aren't clever enough to do this, but it would be a legal optimization.