When working with the C API, use arrays of characters or use strings and convert as needed?

I work with the C API, and many functions accept arguments that are arrays of characters. I heard that using char arrays is now frowned upon. But then again, using c_str() to convert a string to a char over-and-over array seems wasteful.

Is there any reason to do this in one way against another?

+4
source share
5 answers

The call to c_str() is likely to be inline - it is very small in terms of the required code. I would use std::string if that is the only thing holding you back.

Of course, if you are very concerned, this standard tip applies:

  • Profile
  • Read assembly

Also keep in mind that this is micro-optimization; you are likely to spend time developing, worrying about something completely different than what you should worry about.

+10
source

Most std::string implementations probably save the actual string as a C string anyway, so the c_str function is just a built-in function that returns a pointer. Therefore, as a rule, I would say that the correct way is std::string .

Of course, if the string should be changed by the function you are calling, you cannot use the std::string approach. Instead, you will need to make a copy in your own buffer before calling the function, in which case using arrays can be a way.

+2
source

As already mentioned, c_str() will be c_str() . But what was not mentioned, but what I consider to be one of the most important aspects of your question, is that std::string follows the principles of RAII. When using std::string you will not need to forget to free the string or worry about the safety of exceptions. Just make sure that every instance of std::string not destroyed until the C code is executed with a string. This can be especially a problem if std::string is temporary created by the compiler.

If your C function returns a string, you can use vector<char> and set the size to the desired buffer size. That way, you will still follow the principles of C ++ RAII.

+2
source

It depends on what you are doing and what functions the interface does. For one thing, I don't think anyone would recommend converting a string literal to std::string , so you can call c_str on This. On the other hand: any code that builds strings dynamically should use std::string . Functions such as strcpy and strcat are buffer overflow prompts. Between the two, it depends. I would say that the criteria should be ease and security: at any time, something is simpler or safer using std::string , use std::string . So far, what you are doing does not require dynamic allocation of char[] , and things like operator+ will not be used in strings, you can use char[] .

+2
source

There is a very simple reason to use string : it works .

Working with C-lines is a pain:

  • Manually allocating and freeing memory is error prone
  • the interface itself is error prone (no zero character termination, one by one errors, and buffer overflows are common). A.
  • operations are inefficient ( strlen , strcpy and strcat ), since the length must be recalculated at every moment in time

I really see no good reason to ever work with C-strings.

It is such a pain that many platforms provided their own specific operations and that several β€œbest lines” were suggested (oh, the joy of several standards).

+1
source

Source: https://habr.com/ru/post/1395259/


All Articles