Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to declare a C-style string without const? If so, why?

Tags:

c++

c

c-strings

Doing this in C++

char* cool = "cool";

compiles fine, but gives me a warning:

deprecated conversion from string constant to char*.

I would never willfully use a C-style string over std::string, but just in case I'm asked this question:

is it bad practice to declare a C-style string without the const modifier? If so, why?

like image 530
Coffee Maker Avatar asked Jul 25 '16 17:07

Coffee Maker


People also ask

Can you declare string constants in C?

C string constants can be declared using either pointer syntax or array syntax: // Option 1: using pointer syntax. const char *ptr = "Lorem ipsum"; // Option 2: using array syntax.

Can a string be const?

const is compile time. When you use const string you end up not using space for this variable during run time. The compiler uses this value in a way not dissimilar to a macro. When you don't use const string it acts like any other variable and occupies additional space during run time.

What is a valid C string?

A valid C string requires the presence of a terminating "null character" (a character with ASCII value 0, usually represented by the character literal '\0' ). Since char is a built-in data type, no header file is required to create a C string.

Are strings read only C?

Strings using character pointers Using character pointer strings can be stored in two ways: 1) Read only string in a shared segment. When a string value is directly assigned to a pointer, in most of the compilers, it's stored in a read-only block (generally in data segment) that is shared among functions.


2 Answers

Yes, this declaration is bad practice, because it allows many ways of accidentally provoking Undefined Behavior by writing to a string literal, including:

cool[0] = 'k'; strcpy(cool, "oops"); 

On the other hand, this is perfectly fine, since it allocates a non-const array of chars:

char cool[] = "cool"; 
like image 184
aschepler Avatar answered Sep 18 '22 18:09

aschepler


Yes, in C++ you should always refer to string literals with variables of type const char * or const char [N]. This is also best practice when writing new C code.

String literals are stored in read-only memory, when this is possible; their type is properly const-qualified. C, but not C++, includes a backward compatibility wart where the compiler gives them the type char [N] even though they are stored in read-only memory. This is because string literals are older than the const qualifier. const was invented in the run-up to what's now called "C89" -- the earlier "K&R" form of the language did not have it.

Some C compilers include an optional mode in which the backward compatibility wart is disabled, and char *foo = "..."; will get you the same or a similar diagnostic that it does in C++. GCC spells this mode -Wwrite-strings. I highly recommend it for new code; however, turning it on for old code is liable to require an enormous amount of scutwork for very little benefit.

like image 44
zwol Avatar answered Sep 19 '22 18:09

zwol