开发者

Using C++ libraries

whole of our project development is in C. However there are alot of string operations here and there and how much care 开发者_JS百科we make .. we endup having coding errors in string operations and this results most of the time in for buffer overflows ..stack corruptions etc. due to programmers fault.

How nice is this idea to just use C++ string class? So Just introducing CPP for using string library in our project .. is it a good idea ? Would it be a performance impact ? Or Its just not good ..

Please provide your comments.


Why can't you just use the functions in string.h?

Have you considered writing a simple string library in C and extend that when needed? That way, even if you don't get it right immediately, you (different developers) won't be making the same errors over and over.

Or, you could just make your C string library a wrapper of the C++ standard library string class.


One of the nicest things about the history of C++ as a (mostly) superset of C is that you can ease into it just as you suggest. The biggest issue you'll have will be with malloc/free vs. new/delete.

Go ahead and give it a try, you have nothing to lose.


C++ is designed to be mostly back-compatible, so it's usually fairly easy to switch a C project to C++. (Switching your mindset and programming habits are a different story.)

The main things to watch out for would be:

  • If the C code has any functions declared return_type f() (no argument list) but the functions really do take arguments, you'll need to fix them. In C, f() means I'm not telling what the argument types are. In C++, f() in a declaration is the same as f(void).
  • You must compile main() with a C++ compiler. C++ allows some things to happen before and after main, and this makes sure that happens, as your C++ libraries probably expect.
  • If some parts will still be compiled with a C compiler and only parts with a C++ compiler, make sure you understand extern "C" rules.

There are other nitpicky differences where C and C++ behavior is different, but they don't cause problems very often.

Whether it's worth it to start mixing in some C++ is up to you and your team.


In my experience even a compiler upgrade frequently breaks code in unexpected ways. This change is a language change. That is a major change. What are the odds of it not introducing a multitude of bugs, many of which might be really hard for the developers to discover (but easy for the customer).

Also, I'm guessing that the main reason you are using C instead of C++ is team experience. Back in the 90s we learned the hard way that mixing the OO and structured paradigms causes many more problems than sticking to one paradigm or the other. If your team is not experienced with C++ then they will mix in the most inappropriate and bug-causing places.

I'm reasonably certain that changing to a C++ compiler will have a huge negative impact up front. In the long run you'll probably gain but that may be years away.

Your best bet is to write a string library (in C) that helps resolve the issues your are seeing. Then force your developers to use the library. Have you looked into using the alternate safe versions of functions like snprintf, strncpy etc... If your compiler doesn't have those functions already then use that interface and write your own.

If you really want to use C++ then do it on your next project. In the meantime keep a list of problems with this project so you can sell management to the idea that C++ is a better approach than C on that next project. Unless you are writing device drivers or mathematical algorithms that should be an easy sell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜