Reputation: 1540
I came across this piece of code today:
KeepaliveThread::KeepaliveThread(VideoDevice *device)
: Thread()
{
...
startup_lock = new Mutex("KeepaliveThread::startup_lock");
}
Mutex::Mutex(const char *title, int recursive)
{
this->title = title;
...
}
Is the usage of this->title safe? Presumably "KeepaliveThread::startup_lock" is a static area of memory, so it's always safe to read from that location.
Do you think it is acceptable or bad practice?
Upvotes: 0
Views: 44
Reputation: 180720
From [lex.string]
Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n
const char
”, where n is the size of the string as defined below, and has static storage duration (3.7).
And If we go to 3.7 which is [basic.stc] and specificly from [basic.stc.static] we have
All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program (3.6.2, 3.6.3).
So your string literal has static storage duration and the storage duration last for the duration of the program so it is safe to store a const char*
to a string literal
Do note that any attempt to modify a string literal is undefined behavior even if the compiler lets you store a char*
to it.
Upvotes: 1