How to allocate memory for c-string char array?

Therefore, trying to learn how to use C-Strings in C ++, I am having problems with memory allocation.

The idea here is that a new format string is created (s1 + sep + s2) The text I use has provided a heading, so I cannot change it, but I am having problems setting the size of char str []. I get a message stating that sLength is not a constant and therefore cannot be used to set the size of the array. I'm relatively new to C ++, so this is a two-part question.

  • Is this strategy actually allocating memory for a new array?

  • How to set the size of an array correctly if I cannot get a constant value using strlen (char *)?

    char* concatStrings(char* s1, char* s2, char sep){ int sLength = strlen(s1) + strlen(s2) + 3; //+1 for char sep +2 for \0 at end of string char *str = new char[sLength]; strcpy (str, s1); str [sLength(s1)] = sep; strcat (str, s2); return str; } 

Edited, so now I do not get compiler errors, but ...

The function call is here:

  char* str = concatStrings("Here is String one", "Here is String two" , c); cout<< str; 

My conclusion will be:

Here is String onec ================================================ =================================================== =================================================== =================================================== ========

+4
source share
3 answers

The error returns the address of the local variable of the str array. Its scope is inside the concatStrings() function where you declared, and cannot be accessed when the control returns from the function.

To access it from the outside, you need to dynamically allocate memory for a row from the heap using the new operator.

 char* concatStrings(char* s1, char* s2, char sep){ int s1Length = strlen(s1); int sLength = s1Length + strlen(s2) + 2; // +1 for sep and +1 \0 at end of string char* str = new char[sLength]; strcpy (str, s1); // Use strlen here instead of sizeof() str [s1Length] = sep; str [s1Length + 1] = '\0'; strcat (str, s2); return str; } 

And after the program is executed using the string returned from concatStrings it must ensure that memory is freed by calling delete

 char* str = concatStrings(s1, s2, sep); // Do something // Free up memory used by str delete[] str; 

Here it is necessary to use delete [] instead of delete, otherwise this will lead to undefined behavior

I also edited concatStrings() to use strlen instead of sizeof

UPDATE: Thanks for pointing out that we only need to do +2, not +3, and for making sure that we need to add '\ 0' after str1 and sep before strcat

+9
source

You can allocate the resulting memory line dynamically (at runtime, on the heap) using new[] in C ++ (or malloc for more C-style):

 char* concatStrings(const char* s1, const char* s2, char sep) // enforced const correctness { const size_t totalLength = strlen(s1) + strlen(s2) + 2; // +1 for sep char, +1 for '\0' // Dynamically allocate room for the new string (on the heap) char* str = new char[totalLength]; strcpy(str, s1); str[strlen(s1)] = sep; // note that you had a typo with sizeof(s1) here strcat(str, s2); return str; } 

Note that this memory must be released somewhere in your code using delete[] if it was allocated using new[] or free() if it was allocated using malloc() .

It is rather complicated.

You will greatly simplify your code if you use a reliable C ++ string class , for example std::string , and its convenient constructors allocate memory, the destructor automatically frees it, and operator+ and operator+= overloads to concatenate strings. See how code is simplified with std::string :

 #include <string> // for std::string std::string str = s1; str += sep; str += s2; 

(Note that using raw C strings can also make your code more vulnerable to security issues, since you should pay a lot of attention to setting up your target strings correctly, avoid buffer overflows, etc. This is another reason to prefer a reliable RAII class string. e.g. std::string .)

+5
source

sizeof(s1) returns the size of the pointer variable, not the length of the array it points to. Since you know that s1 points to a C-string, you should use the strlen() function.

+1
source

All Articles