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
If there truly was no point to the empty string, then the programmer would not write the instruction at all. The language is loyal and trusting! And will never assume memory you allocate to be "wasted". Even if you are lost and heading over a cliff, it will hold your hand to the bitter end.
I think it'd be interesting to know, just as a curiosity though, that if you create a variable that isn't 'used' later, such as your empty string, the compiler may very well optimize it away so it incurs no cost to begin with. I guess compilers aren't as trusting...
Every named object in C++ has an address. There is even a specific requirement that the size of every type be at least 1 so that T[N]
and T[N+1]
are different, or so that in T a, b;
both variables have distinct addresses.
In your case, s
is a named object of type std::string
, so it has an address. The fact that you constructed s
from a particular value is immaterial. What matters is that s
has been constructed, so it is an object, so it has an address.
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.
An 'empty' string object is still an object - there may be more to its internal implementation than just the memory required to store the literal string itself. Besides that, most C-style strings (like the ones used in C++) are null-terminated, meaning even that "empty" string still uses one byte for the terminator.
s
is a string
object so it has an address. It has some internal data structures keeping track of the string. For example, current length of the string, current storage reserved for string, etc.
More generally, the C++ standard requires all objects to have a nonzero size. This helps ensure that every object has a unique address.
9 Classes
Complete objects and member subobjects of class type shall have nonzero size.
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.