Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: C Strings, C++ Strings?

  1. #1
    telboon is offline Newbie
    Join Date
    May 2008
    Posts
    26
    Rep Power
    0

    C Strings, C++ Strings?

    I'm currently writing in C++. There're 2 kinds of strings that can be used, char[], and String. Which one will be recommended? What are the pros and cons of either choices?

    I'm actually more comfortable with the former, char[]. However, I want to maintain a good habit of programming, so if String is better, I guess it'll be good for me to get used to the better one. So, should I change, or should i not?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    LukeyJ is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    92
    Rep Power
    13

    Re: C Strings, C++ Strings?

    C strings are faster, but insecure. You have to use a lot more code to make these secure. C++ strings are secure, offer more functionality and are easier. There is a slight performance decline as far as I'm aware but not enough. I think they are good practice and your code will be more secure. They are also easier to pass to functions and objects while keeping an efficient use of system resources (use only as much memory as they need).

    Hope this helps.

    Edit: Actually, I was referrering to pointers to char as a C-style string. If you are using character arrays. Any chances in the string size must be within the limits of the size initiated with. If this is over, you need to copy to a new char [] and delete the old one, which is awkward. You will use less memory by using C++ strings.

  4. #3
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: C Strings, C++ Strings?

    Lukey J you are wrong in quite a few things you say there...

    C strings insecure, how? That doesn't make sense.
    C++ strings in no way offer more functionality. What can you do with C++ strings that I can't do with char arrays.
    It is simple to allocate just enough memory for char arrays also so that is a moot point.
    It is in now way "easier" to pass C++ style strings to functions, that doesn't even make sense.
    It's called using a dynamically allocated string (see malloc and realloc). There is no reason to make and delete arrays.
    No, you will not use less memory using C++ strings if you use C char arrays correctly.

    Please don't post blatantly wrong information.

  5. #4
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: C Strings, C++ Strings?

    Quote Originally Posted by telboon View Post
    I'm currently writing in C++. There're 2 kinds of strings that can be used, char[], and String. Which one will be recommended? What are the pros and cons of either choices?

    I'm actually more comfortable with the former, char[]. However, I want to maintain a good habit of programming, so if String is better, I guess it'll be good for me to get used to the better one. So, should I change, or should i not?
    I'd recommend that you prefer std::string in C++ (not that C and C++ are case sensitive -- if there is some 3rd party String out there, that would be a different matter). Advantages would be STL functions overloaded to make you life much simpler and easier when manipulating strings. A disadvantage might be the fact that it is not POD.

  6. #5
    Join Date
    Jul 2006
    Posts
    16,489
    Blog Entries
    75
    Rep Power
    143

    Re: C Strings, C++ Strings?

    Methodz: The reason C-strings are less secure is because you must manually manage their memory. If you do it correctly, there isn't a problem. If you do it incorrectly (which is very easy to do), you have no idea what may happen. The string class has more overhead, but also makes it harder to run beyond the end of your data into other variables.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    LukeyJ is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    92
    Rep Power
    13

    Re: C Strings, C++ Strings?

    Quote Originally Posted by MeTh0Dz|Reb0rn

    C strings insecure, how? That doesn't make sense.
    As winged explained. If someone else modifies your code or uses you code afterwards it's a lot easier for them to mess up the behaviour of c-style strings.

    Quote Originally Posted by MeTh0Dz|Reb0rn
    C++ strings in no way offer more functionality. What can you do with C++ strings that I can't do with char arrays.
    Also true, but this takes usually requires more code in order to protect them from overrunning the end of the string. Joining strings take more code than in c++ style strings.


    Quote Originally Posted by MeTh0Dz|Reb0rn
    It is in now way "easier" to pass C++ style strings to functions, that doesn't even make sense.
    You mean "no" way easier? You have to send an additional parameter, the size of the array while with C++ strings they act a lot more naturally.


    Quote Originally Posted by MeTh0Dz|Reb0rn
    It's called using a dynamically allocated string (see malloc and realloc).
    I'm very aware of this, by the use of malloc and realloc instead of new and delete you are clearly trying to code C in C++, while that is useful in performance critical programs in some instances, it is not consistant.

    Quote Originally Posted by MeTh0Dz|Reb0rn
    There is no reason to make and delete arrays.
    No, you will not use less memory using C++ strings if you use C char arrays correctly.
    When you are joining strings, there is need. This is a lot simpler and managed by c++.

    Methodz, you should be aware by now, that in programming there are many ways to skin a cat. There are no right and wrong answers as to what's better, just advantages and disadvantages and it is a matter of personal taste. You may have to come to terms with that.

  8. #7
    telboon is offline Newbie
    Join Date
    May 2008
    Posts
    26
    Rep Power
    0

    Re: C Strings, C++ Strings?

    however, many standard functions uses char[] as a input parameter. is there a way to pass c++ String to it?

  9. #8
    Join Date
    Jul 2006
    Posts
    16,489
    Blog Entries
    75
    Rep Power
    143

    Re: C Strings, C++ Strings?

    You can use the .c_str() method.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    Join Date
    Apr 2008
    Posts
    789
    Blog Entries
    5
    Rep Power
    24

    Re: C Strings, C++ Strings?

    Quote Originally Posted by LukeyJ View Post
    As winged explained. If someone else modifies your code or uses you code afterwards it's a lot easier for them to mess up the behaviour of c-style strings.
    As long as you know how much space was allocated, you can be extremely safe with external string e.g. in functions.

    Also true, but this takes usually requires more code in order to protect them from overrunning the end of the string.
    Is it that hard to make a loop while(s[i])?
    Joining strings take more code than in c++ style strings.
    No. it just takes one function call: s = strcat(s,s2);
    You mean "no" way easier? You have to send an additional parameter, the size of the array while with C++ strings they act a lot more naturally.
    You don't need to pass the length, C strings are null terminated! Learn some basic C before saying things like that.
    When you are joining strings, there is need. This is a lot simpler and managed by c++.
    Yes, at a cost in speed. Do you think your time is more valuable than the user's?
    Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
    "When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

  11. #10
    LukeyJ is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    92
    Rep Power
    13

    Re: C Strings, C++ Strings?

    Within reason. If performance was that critical we'd all be writing in assembly. I'm very aware of the strcat function. You have to ensure that the source countainer is big enough. It's also better use the secure version of the function. Either way it requires more code, as you can't predict what input the user will give. You have to put more work in to protect c-style strings... if performance is not critical it is better to be on the safe side and use C++ strings.

Closed Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner C# Strings
    By chili5 in forum CSharp Tutorials
    Replies: 3
    Last Post: 08-21-2011, 02:23 PM
  2. C++ Strings
    By xXAlphaXx in forum C and C++
    Replies: 4
    Last Post: 03-04-2010, 03:36 PM
  3. Strings.
    By Aereshaa in forum C Tutorials
    Replies: 4
    Last Post: 10-15-2008, 05:45 AM
  4. VB Strings
    By reniery in forum Visual Basic Programming
    Replies: 10
    Last Post: 10-08-2008, 01:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts