Segmentation fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mihiri
    New Member
    • May 2007
    • 31

    Segmentation fault

    When I use this code it compile without errors ,but when run it ,it shows segmentation fault.I have general idea about segmentation fault,but I can't identify why it happen in this code.Eventhough I use method strcmp (const type) I use the array in the for loop.So I think I chage the location one by one and I didn't go to again write in const memory.This is my code.Can u pls correct this code?

    #include <stdio.h>
    #include <string.h>
    int main()
    {

    char *string[10][2]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
    int i;
    int j;
    int k;
    int ptr;
    k=0;
    printf("0");
    for(i=1;i<10;i+ +)
    {

    for (j=0;j<i;j++)
    {
    ptr = strcmp(string [i][2], string[j][2]);
    if(ptr==0)

    printf("%d",(k) );

    }
    ptr = strcmp(string [i][2], string[j-1][2]);
    if(ptr!=0)
    {
    k++;
    printf("%d",k);
    }
    }
    return 0;
    }

    Thanks
    Mihiri
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Here you defined your array:
    Code:
    char *string[10][2]={"ab","bb","bb","bb","cb","db","eb","fb","fb","gb"};
    Then you try to access its elements:
    Code:
    ptr = strcmp(string [i][2], string[j][2]);
    Array indices start at 0, so '2' is out of bounds.

    Comment

    • mihiri
      New Member
      • May 2007
      • 31

      #3
      Originally posted by scruggsy
      Here you defined your array:
      Code:
      char *string[10][2]={"ab","bb","bb","bb","cb","db","eb","fb","fb","gb"};
      Then you try to access its elements:
      Code:
      ptr = strcmp(string [i][2], string[j][2]);
      Array indices start at 0, so '2' is out of bounds.
      This is a 2 dimensional array.So I use [code]ptr = strcmp(string [i][2], string[j][2]);
      I cant got what u say?
      BTW this code not show any compile errors.It only show segmentation fault in run time.
      Thanks

      Comment

      • scruggsy
        New Member
        • Mar 2007
        • 147

        #4
        Originally posted by mihiri
        This is a 2 dimensional array.So I use [code]ptr = strcmp(string [i][2], string[j][2]);
        I cant got what u say?
        BTW this code not show any compile errors.It only show segmentation fault in run time.
        Thanks
        Yes, it is a two-dimensional array.
        string[10][2] is an array of 10 arrays. Each of those 10 arrays has 2 elements.
        The first element is string[x][0], where 'x' is an index from 0 to 9.
        The second is string[x][1].
        string[x][2] refers to memory outside of the array, which is what results in a segmentation fault. The compiler doesn't check for out-of-bounds condition, but your OS will complain at run-time.
        Hope this makes it clearer.

        BTW, any chance you could delete your other "ssssssssssssss sssssssssss" topic? :)

        Comment

        • mihiri
          New Member
          • May 2007
          • 31

          #5
          Originally posted by scruggsy
          Yes, it is a two-dimensional array.
          string[10][2] is an array of 10 arrays. Each of those 10 arrays has 2 elements.
          The first element is string[x][0], where 'x' is an index from 0 to 9.
          The second is string[x][1].
          string[x][2] refers to memory outside of the array, which is what results in a segmentation fault. The compiler doesn't check for out-of-bounds condition, but your OS will complain at run-time.
          Hope this makes it clearer.

          BTW, any chance you could delete your other "ssssssssssssss sssssssssss" topic? :)
          Eventhough I use ptr = strcmp(string [i][0], string[j][0]); or ptr = strcmp(string [i][1], string[j][1]); it still show segmentation fault.And also cant I take content of array as one.I mean cant I take that 2 elements as one bundle and check.I wanna know are there any method available for that purpose(without cheking element one by one)
          Thank you very much ur help.pls ignore sssssssss topic its a mistake of me

          Comment

          • mihiri
            New Member
            • May 2007
            • 31

            #6
            Originally posted by mihiri
            Eventhough I use ptr = strcmp(string [i][0], string[j][0]); or ptr = strcmp(string [i][1], string[j][1]); it still show segmentation fault.And also cant I take content of array as one.I mean cant I take that 2 elements as one bundle and check.I wanna know are there any method available for that purpose(without cheking element one by one)
            Thank you very much ur help.pls ignore sssssssss topic its a mistake of me
            Hi !
            I change my code as below according ur guidline.But still it show segmentation fault error.
            #include <stdio.h>
            #include <string.h>
            int main()
            {
            //int k;
            //int temp;
            //printf("Enter integer value");
            //scanf("%d", &temp);
            char *string [10][2]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
            int l;
            int i;
            int j;
            int k;
            int ptr;
            k=0;
            printf("0");
            for(i=1;i<10;i+ +)
            {
            for(l=0;l<2;l++ )
            {
            for (j=0;j<i;j++)
            {
            ptr = strcmp(string [i][l], string[j][l]);
            if(ptr==0)

            printf("%d",(k) );

            }
            ptr = strcmp(string [i][l], string[j-1][l]);
            if(ptr!=0)
            {
            k++;
            printf("%d",k);
            }
            }
            }
            return 0;
            }
            Pls help me for solve this problem.
            Thanks
            Mihiri

            Comment

            • mihiri
              New Member
              • May 2007
              • 31

              #7
              Segmentation fault

              This is my code.But it shows segmentation fault in run time.Can you pls help me for solve this problem.
              #include <stdio.h>
              #include <string.h>
              int main()
              {
              char *string [10][2]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
              int l;
              int i;
              int j;
              int k;
              int ptr;
              k=0;
              printf("0");
              for(i=1;i<10;i+ +)
              {
              for(l=0;l<2;l++ )
              {
              for (j=0;j<i;j++)
              {
              ptr = strcmp(string [i][l], string[j][l]);
              if(ptr==0)

              printf("%d",(k) );

              }
              ptr = strcmp(string [i][l], string[j-1][l]);
              if(ptr!=0)
              {
              k++;
              printf("%d",k);
              }
              }
              }
              return 0;
              }

              Comment

              • Meetee
                Recognized Expert Contributor
                • Dec 2006
                • 928

                #8
                Originally posted by mihiri
                Hi !
                I change my code as below according ur guidline.But still it show segmentation fault error.
                #include <stdio.h>
                #include <string.h>
                int main()
                {
                //int k;
                //int temp;
                //printf("Enter integer value");
                //scanf("%d", &temp);
                char *string [10][2]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
                int l;
                int i;
                int j;
                int k;
                int ptr;
                k=0;
                printf("0");
                for(i=1;i<10;i+ +)
                {
                for(l=0;l<2;l++ )
                {
                for (j=0;j<i;j++)
                {
                ptr = strcmp(string [i][l], string[j][l]);
                if(ptr==0)

                printf("%d",(k) );

                }
                ptr = strcmp(string [i][l], string[j-1][l]);
                if(ptr!=0)
                {
                k++;
                printf("%d",k);
                }
                }
                }
                return 0;
                }
                Pls help me for solve this problem.
                Thanks
                Mihiri
                I think there is some problem in looping because I tried this which is error free!

                Code:
                int main()
                {
                char *string [10][2]={"ab","bb","bb","bb","cb","db","eb","fb","fb","gb"};
                int l;
                int i;
                int j;
                int k;
                int ptr;
                k=0;
                char *s1 = string [0][1];
                char *s2 =  string[1][1];
                ptr = strcmp(s1, s2);
                printf("%d\n",ptr);  // output: 0
                
                return 0;
                I am still trying to find where is the error but you also check the loop again

                Regards

                Comment

                • Meetee
                  Recognized Expert Contributor
                  • Dec 2006
                  • 928

                  #9
                  Originally posted by mihiri
                  This is my code.But it shows segmentation fault in run time.Can you pls help me for solve this problem.
                  #include <stdio.h>
                  #include <string.h>
                  int main()
                  {
                  char *string [10][2]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
                  int l;
                  int i;
                  int j;
                  int k;
                  int ptr;
                  k=0;
                  printf("0");
                  for(i=1;i<10;i+ +)
                  {
                  for(l=0;l<2;l++ )
                  {
                  for (j=0;j<i;j++)
                  {
                  ptr = strcmp(string [i][l], string[j][l]);
                  if(ptr==0)

                  printf("%d",(k) );

                  }
                  ptr = strcmp(string [i][l], string[j-1][l]);
                  if(ptr!=0)
                  {
                  k++;
                  printf("%d",k);
                  }
                  }
                  }
                  return 0;
                  }
                  Please Do Not double post.

                  Comment

                  • mihiri
                    New Member
                    • May 2007
                    • 31

                    #10
                    Originally posted by zodilla58
                    I think there is some problem in looping because I tried this which is error free!

                    Code:
                    int main()
                    {
                    char *string [10][2]={"ab","bb","bb","bb","cb","db","eb","fb","fb","gb"};
                    int l;
                    int i;
                    int j;
                    int k;
                    int ptr;
                    k=0;
                    char *s1 = string [0][1];
                    char *s2 =  string[1][1];
                    ptr = strcmp(s1, s2);
                    printf("%d\n",ptr);  // output: 0
                    
                    return 0;
                    I am still trying to find where is the error but you also check the loop again

                    Regards
                    Hi
                    I think the use of strcmp useless if I'm trying element to element .I think the solution is use main(int argc,char*argv) .So I use it but still it show the sme error ie segmentation fault.

                    Comment

                    • scruggsy
                      New Member
                      • Mar 2007
                      • 147

                      #11
                      Originally posted by mihiri
                      Hi !
                      I change my code as below according ur guidline.But still it show segmentation fault error.
                      It's because your array is too small.
                      Remember that when you initialize a literal string of characters, a null character is appended to the end. So this:
                      Code:
                      char string[2] = "ab"; //is this array big enough?
                      ...means "create an array of 2 characters to hold the string "ab". But to mark the end of the string, a third character - null, represented by "\0" - is appended to the string.
                      So your string needs room for that final char:
                      Code:
                      char string[3] = "ab"; //add 1 extra slot for the null byte
                      Code:
                      char *string [10][2]={"ab","bb","bb","bb","cb","db","eb","fb","fb","gb"};
                      See the problem in your code?

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        mihiri-

                        I have sent you a PM, please check it by going to the top right of your browser where it should inform you that you have at least 1 PM.

                        Thanks

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          This code:
                          Originally posted by mihiri
                          char *string [10][2]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
                          Is an array of 10 elements where each element is an array of two char*. That's room for 20 strings but you have initialized only 10 of them.

                          Did you mean to have just an array of 10 strings??
                          [code=c]
                          char *string [10]={"ab","bb","bb ","bb","cb","db ","eb","fb","fb ","gb"};
                          [/code]

                          This will work but the strings are const and therefore can't be changed.

                          Comment

                          • mihiri
                            New Member
                            • May 2007
                            • 31

                            #14
                            breaking for loop

                            This is my code
                            #include <stdio.h>
                            #include <string.h>
                            int main(int argc, char *argv[])
                            {
                            int i;
                            int j;
                            int k;
                            int l;
                            int m;
                            int n;
                            int ptr;
                            k=0;
                            m=0;
                            printf("0");
                            putchar('\n');
                            for(i=2;i<argc; i++)
                            {
                            for (j=1;j<i;j++)
                            {
                            ptr = strcmp(argv [j], argv[i]);
                            if(ptr==0)
                            {
                            for(l=(j-1);l>1;l--)
                            {
                            for(n=1;n<l;n++ )
                            {
                            ptr = strcmp(argv [n], argv[l]);
                            if(ptr==0)
                            m++;
                            }
                            }
                            printf("%d",(j-(m+1)));
                            putchar('\n');

                            }
                            }

                            ptr = strcmp(argv [i], argv[j-1]);
                            if(ptr!=0)
                            {
                            k++;
                            printf("%d",k);
                            putchar('\n');
                            }

                            }

                            return 0;
                            }
                            I want to exit this 2nd for loop(for (j=1;j<i;j++) loop) after it go to the if condition at first time.(The nearest if condition for above mention for loop) I use break for that but it doesnt work properly.Actual ly I use this code for index for string if there are same string I wanna put same index.So can you pls help me.

                            Comment

                            • iWillLiveforever
                              New Member
                              • Feb 2007
                              • 136

                              #15
                              place a while loop with a counter on it

                              while(someRando mVariable<2)
                              {
                              forloop...
                              before end of iteration put this thier
                              someRandomVaria ble++

                              and it should break the for loop

                              you are also able to reset the variable when needed to bring it back to the for loop and through

                              Comment

                              Working...