#Use of std::string vs const std::string& and std::string_view

15 messages · Page 1 of 1 (latest)

proud lichen
#

Hi!~ I made this stupid little app that is basically an "anti fetch" for linux systems, but I'm having trouble with the difference above, i was using std::string_view for stuf like los::get_hostname() but it kept throwing random bytes at me, am I programming wrong? I don't know wether i should try to fix that or remain with std::string, all feedback is appreciated.

#

am i misunderstanding what std::string_view is?

viscid isle
#

std::string_view is a nonowning reference to a std::string (or to some other string-like object). it cannot extend the lifetime of the object it is referencing, and i suspect what you're doing here is trying to do that

proud lichen
#

ohhhh... right

#

i was trying to do that

clear hound
#

might help to know that a string view is just a pointer and a length

#

it's useful as a generic interface for people that have a string object that isn't necessarily std::string

#

it's only purpose is to point at some characters in memory

proud lichen
#

so if the string its pointing to dies

#

it dies with it

#

thats why i was getting random bytes