Thread: Segmentation fault with structs and char pointers

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Says:
    > 1) start the loop at -1.
    > 2) When the loop is done,
    > 2a) increment i
    > 2b) test with strlen1
    > 3) Back to top.
    Nope

    The test is performed before the body of the loop is executed. Think while loop with automatic initialisation and iteration steps thrown in.

    Both these do the same thing.
    for ( i = -1 ; ++i < 10 ; )
    for ( i = 0 ; i < 10 ; i++ )
    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.

  2. #17
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by CornedBee
    Actually the condition IS evalutated before the first run. But since this code is so uncommon, it should be the other. Simply for the sake of readability.
    So right. Don't know what I was thinking.... Must've had a brain cramp.

    Sorry for the misinfo.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #18
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Code:
    bool Equal(char *str1,char *str2) //Is this the problem?
    {
    
     if(strlen(str1) != strlen(str2) )
            return(false);
     for(int i=-1;i<=(int)strlen(str1); ++i)
     {
    	 if(str1[i] != str2[i])
    	 {
    		 return(false);
    	 }
     }
    
     return(true);
    }
    MANY mistakes.

    1. The loop is starting at -1, not 0.
    2. You are doing MASSIVE amounts of iteration -- this algorithm operates on order N^2
    3. What if str2 is shorter than str1? You walk right off the end of the array.
    4. Be const correct. Yes, a const char * will implicitly convert to a char *, but this is a deprecated feature and should NOT be used.

    Code:
    bool Equal(const char *str1, const char *str2)
    {
    
    int i;
     for(i=0;str1[i] && str2[i]; ++i)
     {
    	 if(str1[i] != str2[i]) return false;
     }
    
     if (str1[i] || str2[i]) return false;
    return true;
    }
    A few points to note:

    1) str1[i] && str2[i] -- this keeps the loop going until it hits the end of either string (assuming the return doesn't take place)

    2) str1[i] || str2[i] -- This checks to see if it hit the end of only one of the strings, but not the other (otherwise, it would say "cat" and "catalog" were the same)
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #19
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Talking Feeling defensive...

    Hey! I never said that str1[-1] was beign evaluated! That wasn't me!!! All I said, was that pre or post-incriment only affected the conditional (middle) part of the for-statement.

    And, I said that these run-time crashes are often caused by writing beyond an array's boundries.

    Keybone,
    Your logic was OK, but I still recommend using a more standardized form of the for-statement whenever possible.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    3. What if str2 is shorter than str1? You walk right off the end of the array.
    If that's the case, the first if statement stops the function and the main loop is never executed.
    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

  6. #21
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by CornedBee
    If that's the case, the first if statement stops the function and the main loop is never executed.
    Ah, quite right.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with Structs and pointers
    By pc_doctor in forum C Programming
    Replies: 4
    Last Post: 03-27-2009, 07:08 PM
  2. Replies: 3
    Last Post: 04-19-2004, 06:42 PM
  3. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 07:03 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM