To get a std::string
, pointers of type char
are required, as far as I know. However, to get a pointer, you have to allocate memory (necessarily, or there will be runtime error
, Segmentation Fault
).
However, it may contain too much data or little data; everything depends on the user / file. How does the dynamic memory allocation of a std::string
work?
Simply put: In std::istream
, there is only one way to know the size of the data to be next input . How does std::string
allocate the necessary space, without knowing it and without using unnecessary memory?
Example:
friend std::ifstream& operator>>(std::ifstream& ifs, std::string str)
{
char* dados; //Como eles serão alocados?
ifs >> dados;
str = dados;
}