Segmentation fault when entering a for loop

This is a discussion on Segmentation fault when entering a for loop within the C Programming forums, part of the General Programming Boards category; I'm very new with C so bear with me. Doing an assignment, I've run into a problem entering a for ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Segmentation fault when entering a for loop

    I'm very new with C so bear with me.

    Doing an assignment, I've run into a problem entering a for loop, where I get a segmentation fault.

    Code:
    for (i = 1; i < strlen(str); i++)
    That's what the beginning of the for loop looks like, and it just never enters it. On the previous line, I print the value of strlen(str), which with the example I'm using is 2 (and gets printed as such), so it should be at least entering the loop.

    I just don't understand why I would be getting a segmentation fault here. Maybe if I understood why I might be able to fix it, but I don't even understand why it happens, so that makes it a little harder to fix.

    Any help would be greatly appreciated, and if more information is needed (short of posting the whole code, as I'm pretty sure that violates our academic conduct policy), just let me know.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    There's nothing in that one line that could even begin to help us, if strlen(str) really gives 2 as you say it does.

    What does the loop body look like? Perhaps the seg fault is in there and it just appears to be on that line. Seg faults can be tricky, and depending on how you "determined" where it was, the problem may actually lie elsewhere.

    Posting the smallest, compilable code that produces the problem and giving us a test case to run would really help.

    Also, learning to use a debugger would be a good idea. Then you can print all critical information at the exact time of the seg fault.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by rehpot View Post
    I'm very new with C so bear with me.

    Doing an assignment, I've run into a problem entering a for loop, where I get a segmentation fault.

    Code:
    for (i = 1; i < strlen(str); i++)
    That's what the beginning of the for loop looks like, and it just never enters it. On the previous line, I print the value of strlen(str), which with the example I'm using is 2 (and gets printed as such), so it should be at least entering the loop.

    I just don't understand why I would be getting a segmentation fault here. Maybe if I understood why I might be able to fix it, but I don't even understand why it happens, so that makes it a little harder to fix.

    Any help would be greatly appreciated, and if more information is needed (short of posting the whole code, as I'm pretty sure that violates our academic conduct policy), just let me know.
    No, the problem is elsewhere. You'll need to post more relevant code (or at least describe the problem more clearly)...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Yeah figured more would be needed, just wasn't sure exactly where to start.

    Laptop battery is about to die though so I'll look into it and find some more relevant information when I get home.

    Thanks.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Quote Originally Posted by anduril462 View Post
    Posting the smallest, compilable code that produces the problem and giving us a test case to run would really help.
    OK, in the process of doing this right now, though I might just put it off until tomorrow because I'm exhausted right now. It doesn't appear to be this for loop itself though, as the loop by itself will compile and run properly. I guess this is where I'm getting confused. If it's not the for loop itself, I don't understand why it won't enter the loop.


    Quote Originally Posted by anduril462 View Post
    What does the loop body look like? Perhaps the seg fault is in there and it just appears to be on that line. Seg faults can be tricky, and depending on how you "determined" where it was, the problem may actually lie elsewhere.
    For what it's worth, I determined it to be at the beginning of the loop just by printing something immediately before the loop, and printing something immediately after entering the loop. The second print never happens, so I just assumed it was something wrong with the line posted above, but I guess that's not necessarily the case? Like I said, I'm extremely new to C, and I know next to nothing about segmentation faults, so I didn't really know where to start with this.

    Thanks for the help so far, and thanks in advance for any future help.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Quote Originally Posted by rehpot View Post
    For what it's worth, I determined it to be at the beginning of the loop just by printing something immediately before the loop, and printing something immediately after entering the loop. The second print never happens, so I just assumed it was something wrong with the line posted above, but I guess that's not necessarily the case? Like I said, I'm extremely new to C, and I know next to nothing about segmentation faults, so I didn't really know where to start with this.
    So the when you use printf, it prints to stdout, which conventionally is buffered. You might print something, and before it makes it out of the output buffer and onto the screen, you get a seg fault. It will look like it never hit that printf statement, but it really did. Usually, it's line buffered, so as soon as it hits a new line, it flushes stdout and the data shows up on screen. It will also flush stdout if you ask for input on stdin. A better way is to make debug output go to stderr, which is unbuffered (you can use fprintf instead of printf), but the best is to learn to use a debugger.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Ah, ok that makes a ton of sense, thank you very much. Looks like it IS in fact getting into the for loop, so the problem is later on. I have a good idea of where the problem is now. I think somewhere later on, I might be trying to access an index of an array that doesn't exist, which, in my limited understanding of seg faults, is something that would cause a seg fault.

    Too tired to go through any more of it tonight though, so I'll take a look at it tomorrow and come back if I'm still having trouble. Thanks a lot for the help, that description was exactly what I needed.

    (and learning gdb is next on my list of things to do, so next time I need help I can be more helpful with my question :P)

    Edit: Figured out the seg fault. I was trying to do something like this with an if statement:

    Code:
    if (str[i] == ('.' || '-'))
    Since you can't do that (not sure exactly what goes on when you type that actually, I wasn't getting any errors), it was never entering the necessary if statements and intializing certain variables, which caused some havoc with indices later on. That's something I should probably error-check, but they promised everything would be well-formed and error-checking isn't necessary, so I guess I won't worry about it.

    Thanks a lot for the help, I'd probably still be staring at my for loop wondering what to do if I didn't come here.
    Last edited by rehpot; 02-18-2011 at 10:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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