Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Why) does an empty string have an address?

Tags:

c++

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 ?

like image 516
Baby Dolphin Avatar asked Aug 23 '11 02:08

Baby Dolphin


People also ask

What does an empty string contain?

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 .

What will happen if a string is empty?

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 is the point of an empty string in Python?

What does the empty string "" represent? It tests whether the string has nothing in it. variable begin not would be an empty string.

Is an empty string a valid 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.


2 Answers

Yes, every variable that you keep in memory has an address. As for what the "point" is, there may be several:

  1. Your (literal) string is not actually "empty", it contains a single '\0' character. The 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.
  2. If you are using a language in which strings are mutable (as is the case in C++), then there is no guarantee that an empty string will remain empty.
  3. In an object-oriented language, a string instance with no data associated with it can still be used to call various instance methods on the string class. This requires a valid object instance in memory.
  4. There is a difference between an empty string and a 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.

like image 184
aroth Avatar answered Sep 19 '22 05:09

aroth


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.

like image 27
karlphillip Avatar answered Sep 20 '22 05:09

karlphillip