0

I am wondering whether there is an easy-to-use C string library (similar to string in C++) so that users don't need to deal with memory allocation.

For example, to strcat() two C string, one has to know the sizes of both strings and allocate the memory when necessary. This is a burden to the programmer.

What is the best way to obliviate this kind of burden from the programmer in C?

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • It is still not clear which one is the best or at least which one is the most popular. Is it SDS? – user1424739 Feb 15 '19 at 16:41
  • The simple answer is to use C++ - that is kind of C++'s original purpose - a better C. You don't have to use all of C++ or even OOP to benefit from the string class. – Clifford Feb 15 '19 at 19:54
  • I only use C. I don't think it is a good idea to mix C with C++ headers. After some basic trial, it seems SDS is a good choice as it has been used Redis. – user1424739 Feb 15 '19 at 20:55
  • I was not suggesting mixing C with C++, although they are highly interoperable. If you use C++ compilation, then the code is _all_ C++, even if it is also valid C. But that misses my point. Writing procedural, object-based, or object-oriented code are all valid paradigms in C++. You only need to use as much C++ as you are comfortable with. Without classes, you will not be able to implement a string library with the simplicity of C++'s string class. – Clifford Feb 15 '19 at 23:42
  • Could you show some example code on how to use C++ string in C? Another concern is C++ may do something underneath the hood, which can be hard to understand. If there is any efficiency problem, it will be hard for programmers to figure out why. But C code is much simpler, it basically directly map to machine code, so it should be easier to under the run time behavior. – user1424739 Feb 16 '19 at 10:17
  • That is a different question - and already answered elsewhere on SO no doubt. But in general valid C code compiled using C++ compilation will be no more or less efficient than when compiled with a C compiler. The generated machine code is likely to be more or less identical in most cases. As I said, you don't use C++ string in C, rather your compile your code as C++ and can then introduce C++ specific code where it suits you. Using `std::string` implies some under-the-hood memory management, but that is rather what you were asking for. – Clifford Feb 16 '19 at 13:18
  • But if the majority of my code use C. Would converting C string to std::string cause any overhead? – user1424739 Feb 16 '19 at 15:11
  • Well you don't get something for nothing of course, but any additional library will introduce overhead. If you were to provide a C sting library that provided the functionality of `std::string` (the SDL library suggested for example) it would have a similar overhead in any case, but be syntactically uglier. C strings are very inefficient in any case (see https://www.joelonsoftware.com/2001/12/11/back-to-basics/) and `std::string` handling will often me much faster - your code may be a little larger, and ram usage a little higher, and it will use the heap under the hood. – Clifford Feb 16 '19 at 16:48
  • I would not necessarily discourage anyone from using C, but if you find yourself dissatisfied with C for the reasons you have given, the available solutions in C in terms of libraries are also unsatisfactory. If you want to stick with C, you are choosing to stick with these "inconveniences" (if you perceive them as such). To get what you want you need C++ - if it could be done in C, it would have been. Ultimately C++ is a just bigger tool-bag - all of C then some new tools you may or may not use, including some you should probably certainly avoid. – Clifford Feb 16 '19 at 17:03

0 Answers0