CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: [RESOLVED] Segfault When Using strtok()

    Quote Originally Posted by jsg View Post
    Now looking through my C++ books I can't find a way to tokenize a std::string. Is there a way to do this in the language already, or do I have to make one of my own (not sure how to do this yet, but I think it can be worked out if need be.
    A common way of handling this is using an std::istringstream. It behaves pretty much like any other input stream, but takes an std::string as its source of input.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  2. #17
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Re: [RESOLVED] Segfault When Using strtok()

    Quote Originally Posted by Eri523 View Post
    A common way of handling this is using an std::istringstream. It behaves pretty much like any other input stream, but takes an std::string as its source of input.
    That looks like just what I need, thank you.

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] Segfault When Using strtok()

    Quote Originally Posted by jsg View Post
    That looks like just what I need, thank you.
    Some other issues:
    Code:
    void CDisplay::Message(std::string Message)
    You should pass strings (and most other objects) by reference or const reference. Otherwise, you are passing by value, which means the compiler will make a copy of the value passed, possibly causing bottlenecks.
    Code:
    void CDisplay::Message(const std::string& Message)
    Pass const reference if the string is not changed in the function being called. Also, this no longer needs to be done:
    Code:
    Display.Message((std::string)"Vendor: ", sVendor);
    Instead:
    Code:
    Display.Message("Vendor: ", sVendor);
    You should be minimizing the casting in your program, especially the C-style casts. Anytime you do any sort of cast in C++, you need to think twice before doing so. For example, is it guaranteed that you can cast those values to char*?
    Code:
    std::string sVendor((char*)glGetString(GL_VENDOR));
    Look good and hard at the return type of glGetString. Is it compatible with char*? What if OpenGL changes the underlying definintion to wide characters? Your function will no longer work. So cast with a good deal of thought before doing so -- casting just for the sake of making the compiler behave is a source of a lot of runtime bugs in C++ programs.

    Regards,

    Paul McKenzie

  4. #19
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Re: [RESOLVED] Segfault When Using strtok()

    Quote Originally Posted by Paul McKenzie View Post
    Some other issues:
    You should pass strings (and most other objects) by reference or const reference.
    I understand what you mean by passing by value and reference. Now when you say most objects, does that include things (don't know exactly the correct term to apply) such as shorts and doubles, etc? Or only more complicated things such as arrays maybe?
    Also, this no longer needs to be done:
    Code:
    Display.Message((std::string)"Vendor: ", sVendor);
    Instead:
    Code:
    Display.Message("Vendor: ", sVendor);
    Isn't just "Vendor: " a const char[]? How does the compiler, or me for that matter, know that it's supposed to be a std::string? Oh wait I think I know. Is it because the compiler looks at the argument list and see that the value is supposed to be a std:string?

    And if I see something like "this is some random text" in a program, how can I tell if it's a char[] or std::string?
    You should be minimizing the casting in your program, especially the C-style casts. Anytime you do any sort of cast in C++, you need to think twice before doing so. For example, is it guaranteed that you can cast those values to char*?
    Code:
    std::string sVendor((char*)glGetString(GL_VENDOR));
    Look good and hard at the return type of glGetString. Is it compatible with char*? What if OpenGL changes the underlying definintion to wide characters? Your function will no longer work. So cast with a good deal of thought before doing so -- casting just for the sake of making the compiler behave is a source of a lot of runtime bugs in C++ programs.
    My OpenGL reference says that these functions should always return a GLubyte*, which is apparently the exact same thing as a char*, but with a different name. Something about renaming types for portability or something. So I believe this is not supposed to ever change.

    I know I've been using C-style casts, because I don't fully understand what all the C++ casts do, and when I would use one vs another. C casts seem simple to understand. C++ style casting is an area where I need to build some familiarity and skill.

    I will go through my code and try to apply the points you brought up.

  5. #20
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Re: [RESOLVED] Segfault When Using strtok()

    Quote Originally Posted by Paul McKenzie View Post
    void CDisplay::Message(const std::string& Message)
    This tip alone cut my execution times in half, thanks a ton.

  6. #21
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] Segfault When Using strtok()

    Quote Originally Posted by jsg View Post
    I understand what you mean by passing by value and reference. Now when you say most objects, does that include things (don't know exactly the correct term to apply) such as shorts and doubles, etc? Or only more complicated things such as arrays maybe?
    The usual objects passed by value are the simple types such as ints, doubles, etc. and C++ function objects. Passing by reference is usually done with simple types to indicate that the function can change the value being passed.
    Isn't just "Vendor: " a const char[]? How does the compiler, or me for that matter, know that it's supposed to be a std::string?
    The compiler sees that std::string's constructor is overloaded to take a const char*, so it finds a match and invokes that constructor.
    And if I see something like "this is some random text" in a program, how can I tell if it's a char[] or std::string?
    I don't know what you mean. Please clarify -- a char[] is an array of char, and a std::string is a class that is a wrapper around an "array" of char.
    C casts seem simple to understand.
    And dangerous if used, again, to keep the compiler quiet about errors. If without casting, the compiler gives you an error (the pointer types are incompatible, or an error similar to that), doing 'C' casts to keep the compiler quiet is a very dangerous game to play.

    The classic error is this one:
    Code:
    int **p;
    int q[10][10];
    
    int main()
    {
         p = (int **)q;
    }
    This type of coding is done by a lot of naive C++ (and 'C') programmers. The reason is that the coder believes that a T[][] is the same as T**, but obviously, the compiler does not agree. So what happens is that the coder casts to shut the compiler up, thinking they've won the battle. A huge mistake.

    Regards,

    Paul McKenzie

  7. #22
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Re: [RESOLVED] Segfault When Using strtok()

    Quote Originally Posted by Paul McKenzie View Post
    The compiler sees that std::string's constructor is overloaded to take a const char*, so it finds a match and invokes that constructor.
    I understand now. I've never looked at std:string's constructor, still trying to understand OOP
    I don't know what you mean. Please clarify -- a char[] is an array of char, and a std::string is a class that is a wrapper around an "array" of char.
    Your explanation about std::string's constructor answered that question, even though I wasn't being clear asking.

    I have no more casts left in my code except converting those GLubyte*s into char*s. They rest vanished while I was fixing up my code with the advice I've received so far.

Page 2 of 2 FirstFirst 12

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured