Thread: In GDB no segmentation fault but while running segmentation fault

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    In GDB no segmentation fault but while running segmentation fault

    I am having segmentation fault while running this code Ideone.com - yU80Bd - Online C++0x Compiler & Debugging Tool .. The problem is when I run it in GDB, the code runs fine and excellent. Why is this gdb no segfault but running every other place is segmentation fault?
    here is the problem I am trying to solve Chef and Codes | CodeChef

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when you access txtF[pos] you never check that pos is not out-of-bounds

    also you should not use magic numbers 97 and 122
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's crashing because you don't check for non-alphabetic characters in the loop that counts the frequencies. Something like this will fix it:
    Code:
            for(int i = 0; i < cipherText.size(); ++i) {
                char c = tolower(cipherText[i]);
                if (c < 'a' || c > 'z')
                    continue;
                ++cipherF[c - 'a'].frequency;
            }
    Without that check, the exclamation point at the end of "dummy!" will cause the incrementation of cipherF[-64]. This apparently overwrites something critical and the program crashes when main returns.

    When the program runs under gdb, it has a somewhat different layout and the crash doesn't happen.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 10-17-2013, 06:20 PM
  2. Segmentation Fault
    By waxman83 in forum C Programming
    Replies: 2
    Last Post: 12-09-2010, 12:02 AM
  3. Segmentation fault
    By browser in forum C Programming
    Replies: 8
    Last Post: 12-07-2010, 03:13 PM
  4. segmentation fault
    By petrovgovind in forum C Programming
    Replies: 5
    Last Post: 12-02-2010, 04:44 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM