1
votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sortString(char *s[], int count);

int main(){

        int i;
        char buff[BUFSIZ];

        int count;
       // 's' is a pointer to a char pointer, initially 's' is allocated storage for one char pointer
        char** s= malloc(sizeof(char*));

        printf("Here is the list of unsorted names: \n\n");

        // Keep on reading unlimited number of names until EOF (Ctrl + D) is reached
        for (count = 0; fgets(buff, sizeof(buff), stdin); count++){

           //Step 1: allocate sufficient storage for s[n] to store the content in buff plus one byte for '\0'
           s[count]=malloc(strlen(buff)+1);
           //Step 2: copy the contents of 'buf' to 's[n]'
           strcpy(s[count],buff);
//Step 3: resize the array of pointers pointed to by 's' to increase its size for the next pointer
          *s=realloc(*s,sizeof(char **)*count+2);
          printf("added to the array at s[%i] is : %s",count, s[count]);
        }

       // EOF reached. Now count the number of strings read

        printf("\nCount is %d\n\n", count);

       // Now sort string using sortString function

       // Step 4: implement sortString function for the above-mentioned function declaration
       //sortString(s, count);

       // Step 5: print the list of sorted names.
       int k;
       for(k=0; k<count; k++){
         printf("%s", s[k]);
       }
       // Step 6:free the allocated memory.

        return 0;
}

void sortString(char *s[], int count){
  char * temp;
  int j,k;

  for(j = 0; j < count -1; j++){
    for(k=j+1; k < count; k++){
      if(strncmp(s[k],s[k+1],1)>0){
        temp=s[k];
        s[k]=s[k+1];
        s[k+1] = temp;

      }

    }
  }

}

Full disclosure, this is a homework.

The idea is this : We have a pointer, s, that points to more char pointers, which are char array pointers themselves. We then malloc these pointers in order to fit a string the user enters, then realloc s to fit in more strings.

The malloc and realloc seems fine. I added a line to check what I've added at each array index, and it seems to work just fine.

The issue arises when I try to print it. I'm getting a segfault at step 5, specifically the printf statement. It looks as if s[k] will work for any positive integer before 4, because it segfaults when k is more than 4.

Compiled using gcc -g, ubuntu 18.04 and windows 10. All face the same issue.

1
Suggest sizeof(char **)*count+2 --> sizeof(char *)*(count+2) - chux - Reinstate Monica
@chux has pointed out the culprit. He is so fast:) Here is a live test of your code: segfault.stensal.com/a/w7F5k2UFdGb2o9PL, you can do self-diagnosis for segfault. - stensal

1 Answers

0
votes

In addition to @chux's order of operations fix, you also have a major problem with your realloc statement. You are doing this:

*s = realloc(*s, ...)

Which means you aren't assigning the newly allocated memory to s, you're reallocating and assigning the de-referenced s, essentially s[0]. Which means you're re-allocating the memory allocated for your string, not your array of strings.

I'm not entirely sure why it doesn't hit a segfault earlier, or why it gets through some of the printing, but that's the nature of memory access violation. You may crash at a different time each run, and it's unpredictable.