Re: [Dev-C++] Re: newbie string parsing question
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2004-08-19 20:41:26
|
I have written compilers, XML editors and a number of other string-manipulating things, but 99% of the time when I use C-strings, I work with static buffers or make one-time assignments with strdup(). A lot of the manipulation doesn't even need any new buffers but may use direct pointer substrings. The only realloc code I have are 3 or 4 small functions, about 5 lines each, that I wrote about 15 years ago to insert and remove text in a dynamically allocated buffer. Hardly any bother to write. Hardly any bother to use. For doing reg-exp it doesn't matter if I use my code, or if I use a third-part library, such as the wxRegEx class from wxWidgets. The point is, I don't spend any time fighting any strxx() functions, since they are not enemies. They are one of several useful (good) tools to use, not something to ignore because god has created std::string. There are a number of reasons to keep all options open. Some of them are: - C++ not available for a specific embedded environment. - The application may not include libstd++ because of code size (may cost huge amounts of money if application is updated through mobile phone) - Application must dynamically link with modules written using other compiler. - Application must interact with existing C libraries. - Preformance. std::string can not do anything that C/C++ can't do directly. Direct manipulation can do things that requires extra book-keeping (reference counting etc.) for a std::string solution. For most problems, there are a number of available solutions. Even if we go to the extreme and compare C with C++, most problems can be solved in a good way with or without objects or templates. The C implementation does not automatically need twice as much code or gets twice as many bugs. That is why - if people ask about a "C-type" problem - I don't say they have to use streams, strings etc. I mention that they are available, but at the same time discuss what to think about if the C functions are used. In this case, that scanf() shouldn't be used without specifying maximum sizes for the strings, that it doesn't allocate the strings (except in a gnu extension) and that fgets()/strchr()/strtok() are the other common C-library solution to the problem. People becomes better programmers if they learn multiple solutions to the same problem. /Per W On Thu, 19 Aug 2004, OROSZI Bal=E1zs wrote: > Per Westermark wrote: > > Of course I use the string type in my C++ applications, but I normally > > don't limit myself to _only_ the string type. > > That's the point. A year ago, when I only knew C, I didn't even want to h= ear about C++, > because I was satisfied with my knowledge of "pointer hacking". But I got= myself troubled > whenever I wanted to do string stuff: passing "strings" to functions, usi= ng malloc, > realloc, free, writing my own (rather awkward) string handler library too= k a lot of > productive time away from me. That's why I'm so against using strxxx, bec= ause I used them > extensively, and I only see how stupid I was, that I wasted so much time = on already > existant and well-written things. > > -- > Greetings, > Bal=E1zs > > > > ------------------------------------------------------- > SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media > 100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33 > Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift. > http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285 > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |