Proper way to copy C strings
You could use strdup()
to return a copy of a C-string, as in:
#include <string.h>
const char *stringA = "foo";
char *stringB = NULL;
stringB = strdup(stringA);
/* ... */
free(stringB);
You could also use strcpy()
, but you need to allocate space first, which isn't hard to do but can lead to an overflow error, if not done correctly:
#include <string.h>
const char *stringA = "foo";
char *stringB = NULL;
/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 );
strcpy( stringB, stringA );
/* ... */
free(stringB);
If you cannot use strdup()
, I would recommend the use of strncpy()
instead of strcpy()
. The strncpy()
function copies up to — and only up to — n
bytes, which helps avoid overflow errors. If strlen(stringA) + 1 > n
, however, you would need to terminate stringB
, yourself. But, generally, you'll know what sizes you need for things:
#include <string.h>
const char *stringA = "foo";
char *stringB = NULL;
/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 );
strncpy( stringB, stringA, strlen(stringA) + 1 );
/* ... */
free(stringB);
I think strdup()
is cleaner, myself, so I try to use it where working with strings exclusively. I don't know if there are serious downsides to the POSIX/non-POSIX approach, performance-wise, but I am not a C or C++ expert.
Note that I cast the result of malloc()
to char *
. This is because your question is tagged as a c++
question. In C++, it is required to cast the result from malloc()
. In C, however, you would not cast this.
EDIT
There you go, there's one complication: strdup()
is not in C or C++. So use strcpy()
or strncp()
with a pre-sized array or a malloc
-ed pointer. It's a good habit to use strncp()
instead of strcpy()
, wherever you might use that function. It will help reduce the potential for errors.
If I just initialize stringB as char *stringB[23], because I know I'll never have a string longer than 22 characters (and allowing for the null terminator), is that the right way?
Almost. In C, if you know for sure that the string will never be too long:
char stringB[MAX+1];
assert(strlen(stringA) <= MAX));
strcpy(stringB, stringA);
or, if there's a possibility that the string might be too long:
char stringB[MAX+1];
strncpy(stringB, stringA, MAX+1);
if (stringB[MAX] != '\0') {
// ERROR: stringA was too long.
stringB[MAX] = '\0'; // if you want to use the truncated string
}
In C++, you should use std::string
, unless you've proved that the overhead is prohibitive. Many implementations have a "short string optimisation", which will avoid dynamic allocation for short strings; in that case, there will be little or no overhead over using a C-style array. Access to individual characters is just as convenient as with a C-style array; in both cases, s[i]
gives the character at position i
as an lvalue. Copying becomes stringB = stringA;
with no danger of undefined behaviour.
If you really do find that std::string
is unusable, consider std::array<char,MAX+1>
: a copyable class containing a fixed-size array.
If stringB is checked for equality with other C-strings, will the extra space affect anything?
If you use strcmp
, then it will stop at the end of the shortest string, and will not be affected by the extra space.