5

cppreference uses it to describe std::string_view:

devtut and sodocumentation use it to describe std::string_view as well:

C++17 introduces std::string_view, which is simply a non-owning range of const chars, implementable as either a pair of pointers or a pointer and a length.

And various other questions and answers here on SO reference it, yet I can't find any explanation of what it means.

glibg10b
  • 338
  • 1
  • 11
  • Owning an object means having the responsibility of deleting it, that is calling its destructor. In the case of shared_ptr, which implies multiple owners, its the responsibility of the owners to make sure the last to own the object deletes it. – Uri Raz Aug 07 '21 at 06:26
  • In this context owning means to be responsible for the memory management, especially for freeing allocated memory. – gerum Aug 07 '21 at 06:28
  • What happened when you tried putting `c++ ownership` [into a search engine](https://duckduckgo.com/?q=c%2B%2B+ownership)? Or [`c++ what is ownership`](https://duckduckgo.com/?q=c%2B%2B+what+is+ownership)? – Karl Knechtel Aug 07 '21 at 06:31
  • Does this answer your question? [What is ownership of resources or pointers?](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) I found it as the second result from that second query. – Karl Knechtel Aug 07 '21 at 06:32

3 Answers3

6

You can own a resource, i.e. anything that there is a limited amount of. This is usually memory or system handles. Whatever owns the resource is responsible for releasing it when done using it.

std::unique_ptr and std::shared_ptr are examples of an owning wrapper. It releases their memory when it goes out of use. Same goes for any other RAII class.

std::basic_string_view is non-owning, which is a nice way of saying that it is not bound to actual lifetime of the string in any way, and that, if you are not careful, it may dangle if the string reallocates.

IWonderWhatThisAPIDoes
  • 1,000
  • 1
  • 4
  • 14
2

"Owning X" means being responsible for the lifecycle of X.

In case of a std::string the class contains an array of char. The std::string class is responsible for allocating and deallocating the char array; the whole time the string object owns this array.

In contrast to this std::string_view only "knows about" such an array; It doesn't do any allocations or deallocations with it.

The benefit is that you don't need to do a copy of the array which could get expensive, the drawback is that if you aren't careful, the array could be freed before you stop using the std::string_view which results in undefined behavior so the result could be your program crashing.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • @IWonderWhatThisAPIDoes That's why I wrote "could crash"; clarified it a bit in the answer... – fabian Aug 07 '21 at 06:44
2

An answer to a question about std::span says:

A span<T> is:

...

  • A non-owning type (i.e. a "reference-type" rather than a "value type"): It never allocates nor deallocates anything and does not keep smart pointers alive.

The linked answer provides a good explanation of what a reference type is.

std::string_view and std::span are objects with reference semantics. isocpp's faq provides a good explanation.

glibg10b
  • 338
  • 1
  • 11
  • 1
    The link talks a lot about C++ references as a type, I'd suggest mentioning [objects with reference semantics](https://isocpp.org/wiki/faq/value-vs-ref-semantics), which is what `string_view` and `span` are – IWonderWhatThisAPIDoes Aug 07 '21 at 06:50