(Why) does an empty string have an address?

前端 未结 7 1481
执笔经年
执笔经年 2021-02-19 14:19

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

相关标签:
7条回答
  • 2021-02-19 14:30

    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...

    0 讨论(0)
  • 2021-02-19 14:33

    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.

    0 讨论(0)
  • 2021-02-19 14:35

    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.

    0 讨论(0)
  • 2021-02-19 14:35

    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.

    0 讨论(0)
  • 2021-02-19 14:35

    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.

    0 讨论(0)
  • 2021-02-19 14:48

    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.

    0 讨论(0)
提交回复
热议问题