C++ string c string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mvjohn100
    New Member
    • Mar 2008
    • 57

    C++ string c string

    Being in the development of a project. our group is confused about what shall be the better one c++ string or simple c string allocation. Thanks in advance.

    john
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by mvjohn100
    Being in the development of a project. our group is confused about what shall be the better one c++ string or simple c string allocation. Thanks in advance.

    john
    I would go for c++ string as it provide many useful functions compared to c string

    Raghuram

    Comment

    • mvjohn100
      New Member
      • Mar 2008
      • 57

      #3
      first of all Thanks gpraghuram.

      what about the memory size and memory allocations, speed of C++ string?
      Is it affect the performance of program?

      Is the string class overhead negligible when compared with the hassles of c memory allocation.

      john

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by mvjohn100
        first of all Thanks gpraghuram.

        what about the memory size and memory allocations, speed of C++ string?
        Is it affect the performance of program?

        Is the string class overhead negligible when compared with the hassles of c memory allocation.

        john
        c++ string dosent puts much overhead .(Other members can comment on this)
        It better to use C++ strings as it avoids many common C pointer mistakes

        Raghuram

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by gpraghuram
          c++ string dosent puts much overhead .(Other members can comment on this)
          It better to use C++ strings as it avoids many common C pointer mistakes
          I agree with Raghuram on this. The likely hood of string introducing much (if any) overhead above you allocating you own memory for C strings is low and use of string greatly enhances project maintainablity and reduces the risk of bugs associated with pointers and memory allocation.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            All I know is that std::strings are very easy to work with compared with messy character arrays/pointers in C. I'd trust Banfa and Prag. about the overhead issues (or, in this case, the lack of overhead), so it looks like std::string is the way to go.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Use the C++ string.

              All of the C-string library is deprecated in C++.

              Generally, the C++ sting is faster than coding it yourself. P.J. Plaugher, who wrote the template, has said: The template is optimized for speed. If you think you can do it faster, then think three times.

              I know it's fun to write your own string functions because it's something you know how to do already. I urgently suggest you don't waste time on this. The C++ string is an industry standard making your code more supportable and less prone to bugs. Anything you write will be a one-off, probably not reusable in many programs and harder to understand for maintenance.

              In this area also:
              1) Do not use arrays. Use STL vectors.
              2) Do not write a linked list. Use the STL list template
              3) Do not write a stack. Use the STL stack.
              4) Do not write, queues, deques, priority_queues .
              5) Do not write trees. Use set, multiset, map, multimap.
              6) Do not wrote a sort. Use the sort
              7) Do not write loops where there are algorithms already written.
              8) Buy a copy of Effective STL by Scott Meyers
              9) etc...

              Comment

              • mvjohn100
                New Member
                • Mar 2008
                • 57

                #8
                Thanks for all and this forum for this clear cut solution for the usage of string.It helped me to make a big decision on string manipulation in the current project and the future also.


                John

                Comment

                • mvjohn100
                  New Member
                  • Mar 2008
                  • 57

                  #9
                  Thanks friends
                  Once more in the string problem... if the project is the server program and it requires high efficiency..... Is it still the C++ string is better?

                  john

                  Comment

                  • oler1s
                    Recognized Expert Contributor
                    • Aug 2007
                    • 671

                    #10
                    I have a question for you. Let's pretend the C++ string class did not exist. What kind of code would you end up writing?

                    On further reflection, consider what you probably would end up doing. You would end up making a mini C++ string class, following all the good programming techniques like RAII and so on, right? But wait, there's this whole C++ STL library, and you might want to make your C++ string class compatible with that, so you can take advantage of the C++ standard library. Do you see where I'm going with my logic?

                    Moreover, you're sitting around debating possible benefits to having a formal string class instead of the base C char arrays. Step 1, get code to work. Let's see you get working code before you talk about performance.

                    Comment

                    • mvjohn100
                      New Member
                      • Mar 2008
                      • 57

                      #11
                      Thanks for the answer.
                      Definitely a new "our own string class" is not a feasible one.

                      John

                      Comment

                      Working...