Thread: char causes segfaults more than strings?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    char causes segfaults more than strings?

    Hello all,

    I am very new to c++, (but have been a developer for 15 years) and have a question.

    I work with other c++ programmers. One loves chars the other says they could cause seg faults and I should use strings.

    What are your thoughts?

    Thanks,
    Michael

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by TheBigOnion View Post
    Hello all,

    I am very new to c++, (but have been a developer for 15 years) and have a question.

    I work with other c++ programmers. One loves chars the other says they could cause seg faults and I should use strings.

    What are your thoughts?

    Thanks,
    Michael
    If you know how to use character arrays, they're perfectly safe. If you don't, then not only can your application segfault but you can add serious security flaws to your application.
    But in my opinion, you have to learn about char arrays.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you are talking about using pointers to char to work with null terminated strings. If so, it is true that working with them can be more error prone than working with string objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    std::string does things with you (manage memory, ensure that enough memory is allocated etc). With C style strings you'd have to take care of these things yourself. You'd write more code and have more chances to screw things up.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char compile error
    By dallypost in forum C Programming
    Replies: 10
    Last Post: 03-18-2010, 03:54 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM