In C ++, is it good practice to initialize a char array with a string literal?

in C ++ is it good practice to initialize a char array with a string? eg:

char* abc = (char *) ("abcabc");

      

I see a lot of them in my colleague. Should I change it to correct practice? such as

std::string abc_str = "abcabc";
const char* abc= abc_str .c_str();

      

+3


source to share


3 answers


This statement

char* abc = (char *) ("abcabc");

      

just bad. String literals in C ++ are of constant character array types. Thus, a valid ad would look like

const char *abc = "abcabc";

      

Note. In C, you can actually write

char *abc = "abcabc";

      

However, string literals are immutable. Any attempt to modify a string literal results in undefined behavior.

By the way, there is no character array that is initialized with a string literal. Perhaps you mean the following

char abc[] = "abcabc";

      



The use of a standard class std::string

does not exclude the use of character arrays and, furthermore, pointers to string literals.

Please note that these ads

const char *abc = "abcabc";

      

and

std::string abc_str = "abcabc";
const char* abc= abc_str .c_str();

      

are not equivalent. With respect to the first literals, declaration lines have a static storage duration, and their addresses do not change during program execution. In the second declaration, the pointer abc

points to dynamically allocated memory that can be reallocated if the object abc_str

is changed. In this case, the pointer will be invalid.

Also the first declaration assumes that the array (string literal) pointed to by the pointer will not be modified. The second declaration assumes that an object of type std :: string will be modified. Otherwise, it makes no sense to specify an object of type std :: string instead of a pointer.

So the meanings of the ads are just different.

+12


source


char* abc = (char *) ("abcabc");

      

This is bad. Do not do that.

You are handling a string literal that should not be modified, how can it be modified.

Thereafter



abc[0] = 'd';

      

will be OK in the compiler, but won't work at runtime. What do you need to use:

char abc[] = "abcabc";

      

This will create a modifiable array.

+5


source


Both of them are bad.

char* abc = (char*) ("abcabc");

      

A string literal is a constant and, as such, can be stored in write-protected memory. So writing to it might crash your program, it's undefined behavior.

Instead of discarding the constant, you should save it const

and make a copy if you want to edit its contents.

const char* abc = "abcabc";

      

Others should be avoided:

std::string abc_str = "abcabc";
const char* abc = abc_str.c_str();

      

Keeping the constant is good, but if the string has changed, it can be reallocated elsewhere in memory, leaving your pointer dangling.

Also in the pre code, the C++11

pointer ceases to be valid, the second is assigned because there is no guarantee that it is not temporary.

Better to call it abc_str.c_str()

every time.

Most likely, because c_str()

is such a trivial operation, it will be optimized by the compiler, making it as efficient as using a raw pointer.

Instead of using the ones you should be doing std::string

completely. If you absolutely need it const char*

(for old legacy code) you can get it with c_str()

.

std::string abc_str = "abcabc"; // this is perfect why do more?

old_horrible_function(abc_str.c_str()); // only when needed

      

+3


source







All Articles