need some help with Segmentation fault

This is a discussion on need some help with Segmentation fault within the C Programming forums, part of the General Programming Boards category; This program is a new assignment for array and string, however the program shows segmentation error after first execution. it ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    need some help with Segmentation fault

    This program is a new assignment for array and string, however the program shows segmentation error after first execution.

    it says Segmentation fault.
    i check the out put from the function check.
    it print quiet normal.

    i just spent 6 hour straight in the lab to fix the error.
    however it is Friday , i can not get any help from TA.
    If you guy can spare some time , it will be really appreciated.









    Code:
    
    #include <stdio.h>
    #include <math.h>
    #include<string.h>     
    #include<stdbool.h>
    #include<malloc.h>
    int main(void)
    {
    
    
      void record(char *response,char *guess, int NOG,int length,char **matrix);
      void check(char *key, char  *guess,char *response,int length);
    
    
      int t;
        
     
    
    
    
    
      printf("Enter the pattern length");
      int length;
      scanf("%d",&length);
    
    
      printf("Enter the number of guesses");
      int limit;
      scanf("%d",&limit);
    
    
      char *response; 
      response=(char *)malloc((length+1)*sizeof(char*));
    
    
    
    
      char guess[length];
    
    
     
     
      printf("Input the key pattern with no spaces:");
      char key[length+1];
      scanf("%s",key);
    
    
      int NOG=0;
      //response to generate the bww thing
    
    
      char **matrix;//for printing the result
    matrix=(char **)malloc(2*limit*sizeof(char*));
     
    for(t=0;t<limit;t++)
      matrix[t]=(char *)malloc((length*2+1)*sizeof(char));
                     
    //making 2 dimentional array 'matrix'
    
    
    
    
    
    
     int j,w,k,g,c;
    
    
    do
    
    
        {  printf("Input a guess pattern with no space:");
          
          scanf("%s",guess);
        
          check(key,guess,response,length);
      int counts;
      counts=0;
     
      for(j=0;j<length;j++)
        {matrix[NOG][j]=response[j];
         
          }
      matrix[NOG][j]=' ';
      
      for(k=length+1;k<length*2+1;k++)
        {matrix[NOG][k]=guess[k-length-1];
          
            }
    
    
      
      for(w=0;w<NOG+1;w++)
        {
        printf("%d: ",w+1);
        for(g=0;g<length*2+1;g++)
          {printf("%c",matrix[w][g]);
          }
     
        printf("\n");
        }
    
    
    
    
    
    
    
    
        NOG++;
    
    
        }
     while(strcmp(guess,key) && NOG!=limit);
     return 0;
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    void check(char *key, char  *guess,char *response,int length)
    
    
      {
        int j,k,n,m,t,z,x,w,i;
        k=0;
       
      int counts=0;
      bool *badj;
    for(t=0;t<length;t++)
      badj[t]=(bool *)malloc((length)*sizeof(bool));
     for(n=0;n<length;n++)
       badj[n]=1;
     
    
    
    bool *bads;
     for(w=0;w<length;w++)
       bads[w]=(bool *)malloc((length)*sizeof(bool));
    
    
    
    
     for(z=0;z<length;z++)
       bads[z]=1;
    
    
        for(x=0;x<length;x++)
          {if(key[x]==guess[x])
          { response[k]='b';counts++;
                    bads[x]=0;
                    k++;
          }
          }
     
    
    
    
    
    
    
    
    
    
    
        
    
    
    
    
    
    
    
    
        for(i=0;i<length;i++)
          {  if(bads[i]==0)
        continue;
    
    
    
    
        for(j=0;j<length;j++)
        {if(badj[j]==0)
            {continue;}
          
          else if(key[i]==guess[j])
            {response[k]='w';counts++;
              badj[j]=0;
              k++;
    
    
              }
        }
          }
       for(m=counts;m<length;m++)
         {response[k]='.';k++;}
       response[k]='\0';
       printf("%s",response);
      }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    well it is 2 AM right now just cant sleep before this program works

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    First, your indentation needs work.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include<string.h>
    #include<stdbool.h>
    #include<stdlib.h>  //!! malloc is here, not in malloc.h
    int main(void)
    {
      void record(char *response, char *guess, int NOG, int length, char **matrix);
      void check(char *key, char *guess, char *response, int length);
      int t;
    
      printf("Enter the pattern length");
      int length;
      scanf("%d", &length);
    
      printf("Enter the number of guesses");
      int limit;
      scanf("%d", &limit);
    
      char *response;
      response = (char *) malloc((length + 1) * sizeof(char *));
      char guess[length];
    
      printf("Input the key pattern with no spaces:");
      char key[length + 1];
      scanf("%s", key);
    
      int NOG = 0;
      //response to generate the bww thing
    
      char **matrix;                //for printing the result
      matrix = (char **) malloc(2 * limit * sizeof(char *));
      for (t = 0; t < limit; t++)
        matrix[t] = (char *) malloc((length * 2 + 1) * sizeof(char));
    
    //making 2 dimentional array 'matrix'
      int j, w, k, g, c;
    
      do
      {
        printf("Input a guess pattern with no space:");
        scanf("%s", guess);
    
        check(key, guess, response, length);
        int counts;
        counts = 0;
    
        for (j = 0; j < length; j++) {
          matrix[NOG][j] = response[j];
    
        }
        matrix[NOG][j] = ' ';
    
        for (k = length + 1; k < length * 2 + 1; k++) {
          matrix[NOG][k] = guess[k - length - 1];
        }
    
        for (w = 0; w < NOG + 1; w++) {
          printf("%d: ", w + 1);
          for (g = 0; g < length * 2 + 1; g++) {
            printf("%c", matrix[w][g]);
          }
          printf("\n");
        }
    
        NOG++;
      }
      while (strcmp(guess, key) && NOG != limit);
      return 0;
    }
    
    void check(char *key, char *guess, char *response, int length)
    {
      int j, k, n, m, t, z, x, w, i;
      k = 0;
    
      int counts = 0;
      bool *badj;
      for (t = 0; t < length; t++)
        badj[t] = (bool *) malloc((length) * sizeof(bool));
      for (n = 0; n < length; n++)
        badj[n] = 1;
    
      bool *bads;
      for (w = 0; w < length; w++)
        bads[w] = (bool *) malloc((length) * sizeof(bool));
    
      for (z = 0; z < length; z++)
        bads[z] = 1;
    
      for (x = 0; x < length; x++) {
        if (key[x] == guess[x]) {
          response[k] = 'b';
          counts++;
          bads[x] = 0;
          k++;
        }
      }
    
      for (i = 0; i < length; i++) {
        if (bads[i] == 0)
          continue;
        for (j = 0; j < length; j++) {
          if (badj[j] == 0) {
            continue;
          }
          else if (key[i] == guess[j]) {
            response[k] = 'w';
            counts++;
            badj[j] = 0;
            k++;
          }
        }
      }
      for (m = counts; m < length; m++) {
        response[k] = '.';
        k++;
      }
      response[k] = '\0';
      printf("%s", response);
    }
    Second, compile with lots and lots of warnings enabled.
    Code:
    $ gcc -W -Wall -Wextra foo.c
    foo.c: In function ‘main’:
    foo.c:45:9: warning: variable ‘counts’ set but not used [-Wunused-but-set-variable]
    foo.c:37:19: warning: unused variable ‘c’ [-Wunused-variable]
    foo.c: In function ‘check’:
    foo.c:80:9: warning: ‘badj’ may be used uninitialized in this function [-Wuninitialized]
    foo.c:86:9: warning: ‘bads’ may be used uninitialized in this function [-Wuninitialized]
    The last two are killing your program.
    Code:
      bool *badj;
      for (t = 0; t < length; t++)
        badj[t] = (bool *) malloc((length) * sizeof(bool));
    You need to make badj point to something, before using badj[t]


    Third, this is C code, so you can remove the casting on the malloc return.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    thank you for your reply Salem.
    Really helps me a lot
    i made some big changes on my code

    Code:
    bool *badj=null;
    
    bool *bads=null;
    As you mentioned about my point error.
    i believe there are serial problem like this in my code
    Code:
     char *response;
    char **matrix;
    char key[length+1] ;
    are those considered to be mistakes?
    if not what is the differenence between them and
    Code:
    bool* bads



    TA always warm us about the string and array, but i still don not understand what is the difference between those 2
    and the '0\' charactor at the end of the string,
    do i have to do this every time after reading the input in? if not what kind of condition do i have to worry about string?
    Code:
    response[k] = '\0';
    and can i do this to the string after user input the guess? when the string guess already has value inside. or do i have to use scanf("c")with for loop to scanf it seperately?
    Code:
     printf("Input a guess pattern with no space:");    scanf("%s", guess);
    
    if it is now legal, please share some thoughts about how to solve it.

    i am really new to string, forgive me for asking silly questions
    thank you

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by zhangz64 View Post
    i made some big changes on my code
    Code:
    bool *badj=null;
    bool *bads=null;
    That won't help (BTW: the macro is written NULL (all caps); C is case sensitive).

    But could you please explain what do you want to achieve with these lines?
    Code:
    bool *badj;
    for (t = 0; t < length; t++)
        badj[t] = (bool *) malloc((length) * sizeof(bool));
    for (n = 0; n < length; n++)
        badj[n] = 1;
      
    bool *bads;
    for (w = 0; w < length; w++)
        bads[w] = (bool *) malloc((length) * sizeof(bool));
    for (z = 0; z < length; z++)
        bads[z] = 1;

    Quote Originally Posted by zhangz64 View Post
    Code:
     char *response;
    char **matrix;
    char key[length+1] ;
    are those considered to be mistakes?
    Depends on what you want to do with these variables.

    Quote Originally Posted by zhangz64 View Post
    TA always warm us about the string and array, but i still don not understand what is the difference between those 2
    and the '0\' charactor at the end of the string,
    An array is a container which holds a defined number of objects of the same type.
    A string in C is an array of type char terminated by the null character ('\0').

    Quote Originally Posted by zhangz64 View Post
    do i have to do this every time after reading the input in? if not what kind of condition do i have to worry about string?
    Code:
    response[k] = '\0';
    and can i do this to the string after user input the guess? when the string guess already has value inside. or do i have to use scanf("c")with for loop to scanf it seperately?
    [code] printf("Input a guess pattern with no space:"); scanf("%s", guess);
    scanf("%s", guess) adds the null character to the input, thus you don't need to add it yourself. If "guess" contains already something it will be overwritten.
    If you read the input character by character, then you would have to add the null character.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By asherly7 in forum C Programming
    Replies: 3
    Last Post: 11-05-2010, 08:15 PM
  2. Segmentation fault
    By BigAll in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 05:25 AM
  3. Segmentation Fault
    By blaksheep423 in forum C Programming
    Replies: 11
    Last Post: 12-07-2006, 04:28 AM
  4. Segmentation fault
    By tameeyore in forum C Programming
    Replies: 4
    Last Post: 02-26-2005, 12:49 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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