When I do this examples on section 5.6, which is a sort program to sort a set of textlines into alphabetic order: I just write the code and start to run it, and I found something not quite right:
#include <stdio.h> #include <string.h> #include "alloc.c" #define MAXLENGH 100 /* max length per line */ #define MAXLINES 100 /* max lines to be sorted */ char *lineptr[MAXLINES];/* pointers to the text lines */ int readlines(char *lineptr[], int nlines); void writelines(char *lineptr[], int nlines); void strcopy(char *s, char *t); void qsort(char *linestr[], int left, int right); int stringcmp(char *, char *); /* sort input lines */ int main(){ int nlines; /* the number of input lines read */ if ((nlines = readlines(lineptr, MAXLINES)) > 0){ qsort(lineptr, 0, nlines -1); writelines(lineptr, nlines); return 0; }else{ return 1; } } int my_getline(char *, int); /* read input lines */ int readlines(char *lineptr[], int maxlines){ int len, nlines; char *p, line[MAXLENGH]; nlines = 0; while((len = my_getline(line, MAXLENGH)) > 0){ //if (nlines >= MAXLENGH || (p = alloc(len)) == NULL) // return -1; //else { line[len -1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; } return nlines; } void writelines(char *lineptr[], int lines){ while(lines-- > 0) printf("%s\n", *lineptr++); } int my_getline(char *s, int limit){ int i, c; for (i = 0; i < limit -1 && (c = getchar())!= '\n' && c != EOF;i++) *s++ = c; if (c == '\n'){ *s++ = '\n'; i++; } *s = '\0'; return i; } /* qsort:sort v[left]...v[right] into increasing order */ void swap(char *v[],int i, int j); void qsort(char *v[], int left, int right){ int i, last; if (left >= right)/* do nothing if the array contains less than 2 elements*/ return; swap(v, left, (left + right) / 2); last = left; for (i = left + 1; i <= right; i++){ if (stringcmp(v[i], v[left]) > 0){ swap(v, ++last, i); } } swap(v, last, left); qsort(v, left, last -1); qsort(v, last + 1, right); } * qsort:sort v[left]...v[right] into increasing order */ void swap(char *v[],int i, int j); void qsort(char *v[], int left, int right){ int i, last; if (left >= right)/* do nothing if the array contains less than 2 elements*/ return; swap(v, left, (left + right) / 2); last = left; for (i = left + 1; i <= right; i++){ if (stringcmp(v[i], v[left]) > 0){ swap(v, ++last, i); } } swap(v, last, left); qsort(v, left, last -1); qsort(v, last + 1, right); } int stringcmp(char *s, char *t){ while (*s++ == *t++) ; if (*t == '\0') return 0; else return 1; } void swap(char *v[], int i, int j){ char *temp; temp = v[i]; v[i] = v[j]; v[j] = temp; } void strcopy(char *s, char *t){ while(*s++ = *t++) ; } For most of part, there is no need to worry about, but for this line:
line[len -1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; when it comes to strcpy, which is to copy a string, it will cause segmentation fault;
A string can be initialized in different ways Segmentation fault then I write another program to verify my thought:
#include <stdio.h> #include <string.h> int main(){ char *p; char line[] = {'h','e','l','l','o','\0'}; strcpy(p, line); return 0; } It will give me same error, I assume the code in this book is correct, but It does run into crash, (by the way I only currently care about the segmentation error part, for other parts, ignore them please) am I missing some code part? any advice is thankful!
https://stackoverflow.com/questions/66881430/kr-c-book-a-stripped-down-version-of-unix-sort March 31, 2021 at 11:56AM
没有评论:
发表评论