I guessed no, but this output of something like this shows it does
string s="";
cout<<&s;
what is the point of having empty string with an address ? Do you think that should not cost any memory at all ?
An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .
Definition and Usage The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.
What does the empty string "" represent? It tests whether the string has nothing in it. variable begin not would be an empty string.
The empty string is a legitimate string, upon which most string operations should work. Some languages treat some or all of the following in similar ways: empty strings, null references, the integer 0, the floating point number 0, the Boolean value false, the ASCII character NUL, or other such values.
Yes, every variable that you keep in memory has an address. As for what the "point" is, there may be several:
std::string
object that is created to contain it may allocate its own character buffer for holding this data, so it is not necessarily empty either. null
string. Sometimes the distinction can be important.And yes, I very much agree with the implementation of the language that an "empty" variable should still exist in and consume memory. In an object-oriented language an instance of an object is more than just the data that it stores, and there's nothing wrong with having an instance of an object that is not currently storing any actual data.
Following your logic, int i;
would also not allocate any memory space, since you are not assigning any value to it. But how is it possible then, that this subsequent operation i = 10;
works after that?
When you declare a variable, you are actually allocating memory space of a certain size (depending on the variable's type) to store something. If you want to use this space right way or not is up to you, but the declaration of the variable is what triggers memory allocation for it.
Some coding practices say you shouldn't declare a variable until the moment you need to use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With