Like Tree4Likes

Comparing a string that might not exist?

This is a discussion on Comparing a string that might not exist? within the C++ Programming forums, part of the General Programming Boards category; I'm writing a parser that gets a line from a file and separates it (by spaces) into a string vector. ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    23

    Comparing a string that might not exist?

    I'm writing a parser that gets a line from a file and separates it (by spaces) into a string vector. Unfortunately there's no way to know how long the string will be. And to figure out what the program needs to do next, it has to compare the contents of the vector with literals.

    Code:
    Split * cSplit = new Split(strLine);
    vector<string> vstrChunks;
    
    vstrChunks.push_back(cSplit->first());
    while(!cSplit->islast())
        vstrChunks.push_back(cSplit->next());
    delete cSplit;
    
    if(vstrChunks[4] == "blah") {
        //The segfault is caused here only when the 4th chunk doesn't exist in memory.
    }
    How can I fix it so it checks if the memory exists before comparing?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Look at vstrChunks.length()
    screennameless likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    23
    If you mean vector::size, then thank you! I'm not quite sure how I missed that.
    Salem likes this.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Is there any reason to use new here?
    Don't forget that there is an at method. Use it for security reasons.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    at() would only throw and leak the newed memory I think.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Better than getting a buffer overrun.
    Plus using smart pointers (or avoiding new altogether!--I don't see a reason for it here), you won't get memory leaks either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    >> Better than getting a buffer overrun.

    That's why you check the length.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Which is error prone. I did not suggest replacing check length with at(). I suggested complementing it with at().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    If it's so error prone then the only thing your doing is hoping you didn't screw it up, so at() doesn't throw when the index fails a bounds check by at().

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    at() is implemented in the standard library, which (hopefully) is more tested and bug free than your code. I would trust it over any other programmer's code any day.
    So yes, it's good to have a second layer of defense, especially when performance is not of concern.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How is an exception that you don't catch a second layer of defense, while a debug assertion (which operator[] has in every implementation I know) is not?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I didn't say a debug assertion isn't a second layer of defense, so long as it aborts in release builds too. I simply do not know of any implementations that does this expect for MSVC. That said, if they do, and if you can guarantee that your code will compiled on such a compiler, then it is an acceptable second layer of defense.
    The at() method is guaranteed to work always, which is why I suggested it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    A debug assertion isn't done in release by definition, but that's just nitpicking.

    I just don't consider an exception an acceptable response to a programming error.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    It isn't an acceptable response. It's a bug. But you wouldn't want your financial to be corrupted because of a "programming error."
    Hence, the second layer defense is to stop such things from happening. But it doesn't relieve you of the responsibility to write correct and bug free code (it just means we are imperfect and introduce such bugs sometimes).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    Quote Originally Posted by Elysia
    I simply do not know of any implementations that does this expect for MSVC. That said, if they do, and if you can guarantee that your code will compiled on such a compiler, then it is an acceptable second layer of defense.
    Technically, you don't even need to be a compiler vendor to write the standard library. If you use STL-Port for example, you can get the same protection from any compiler that can compile it, see STLport: Debug Mode.

    And if you are comfortable with a compiler vendor's implementation, googling "foo debug mode" can be enlightening. They probably call their implementation something different from the name of their compiler though.

    And for good measure: Chapter..17...Debug Mode

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  2. String comparing in C
    By petrock6 in forum C Programming
    Replies: 10
    Last Post: 12-05-2008, 05:08 PM
  3. Comparing Value of Int to String
    By Xeavor in forum C Programming
    Replies: 6
    Last Post: 12-12-2004, 03:44 PM
  4. string comparing
    By pode in forum C++ Programming
    Replies: 5
    Last Post: 11-18-2002, 05:07 PM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21