Coredump while parsing partitions

I am doing homework, which is read in a book. First, a line is read and a pointer pointing to that line. Then the paragraph function is read in lines and stores their address in an array of pointers. Now I am reading a chapter (a paragraph recognized as the next broken line). He should name get_paragraph()and store the address of the paragraphs until a new chapter appears.

A new chapter is the only time in a book where the first character in a line is not space. I think it was if I had problems with the code. All functions up to this point work. Hope I have provided enough information. The code compiles, but flushes the kernel at startup.

I am a student and studying, please be kind. Thank.

char*** get_chapter(FILE * infile){

    int i=0;

    char **chapter[10000];//an array of pointers 
    // Populate the array
    while(chapter[i]=get_paragraph(infile)) { //get address store into array
      if(!isspace(**chapter[0])){    //check to see if it is a new chapter<---problem line?
        // save paragraph not used in chapter using static to put into next chapter
        break;
      }
      i++;//increment array
    }
    //add the null
    chapter[++i]='\0';//put a null at the end to signify end of array
    //Malloc the pointer
    char**(*chap) = malloc(i * sizeof(*chap));//malloc space
    //Copy the array to the pointer
    i=0;//reset address
    while(chapter[i]){//while there are addresses in chapter
      chap[i] = chapter[i++];//change addresses into chap
    }
    chap[i]='\0';//null to signify end of chapter
    //Return the pointer
    return(chap);//return pointer to array
  }

For those who would rather see without comment:

 char*** get_chapter(FILE * infile){

    int i=0;

    char **chapter[10000];
    while(chapter[i]=get_paragraph(infile)) { 
      if(!isspace(**chapter[0])){   
        break;
      }
      i++;
    }
    chapter[++i]='\0';
    char**(*chap) = malloc(i * sizeof(*chap));//malloc space
    i=0;
    while(chapter[i]){
      chap[i] = chapter[i++];
    }
    chap[i]='\0';
    return(chap);
  }
+3
4

for while s? , , .

, :

while(chapter[i]=get_paragraph(infile)) { 
  if(!isspace(**chapter[0])){   
    break;
  }
  i++;
}
chapter[++i]='\0';

-, chapter[i] chapter[0]? , chapter[i] , chapter. , , , - for, .

-, i while, chapter[++i]. i , , . ++i , , i++ , , i. , , , , , . .

(, , ), '\0'? , ? . , 0, '\0'. , , '\0' , .

+1

.

char*** get_chapter(FILE * infile) {
  int i=0;

  // This is a zero length array!
  // (The comma operator returns its right-hand value.)
  // Trying to modify any element here can cause havoc.
  char **chapter[10,000];

  while(chapter[i]=get_paragraph(infile)) { 
    // Do I read this right? I translate it as "if the first character of 
    // the first line of the first paragraph is not whitespace, we're done."
    // Not the paragraph just read in -- the first paragraph.  So this will exit
    // immediately or else loop forever and walk off the end of the array
    // of paragraphs.  I think you mean **chapter[i] here.
    if(!isspace(**chapter[0])){   
      break;
    }
    i++;
  }

  // Using pre-increment here means you leave one item in the array uninitialized
  // which can also cause a fault later on.  Use post-increment instead.
  // Also '\0' here is the wrong sort of zero; I think you need NULL instead.
  chapter[++i]='\0';

  char**(*chap) = malloc(i * sizeof(*chap));
  i=0;
  while(chapter[i]) {
    // This statement looks ambiguous to me.  Referencing a variable twice and
    // incrementing it in the same statement?  You may end up with an off-by-one error.
    chap[i] = chapter[i++];
  }

  // Wrong flavor of zero again.
  chap[i]='\0';
  return(chap);
}
+2

:

if(!isspace(**chapter[i])){

chapter[i] char, char . , **chapter[i] i. chapter[0] .

0

Have you tried one step, although it is in gdb, and from time to time delete local variables to see the current state? This is a good way to learn. You might want to add a few additional intermediate variables that will also automatically reset the “information locators” (pointers to the current XXX, where XXX are the various elements in your hierarchy)

I assume the GNU environment:

% gcc -g homework.c -o hw
% gdb hw
(gdb) b 10
(gdb) r
(gdb) info locals
(gdb) n
(gdb) info locals
...

Replace “10” with the appropriate line number next to the start of the function.

0
source

Source: https://habr.com/ru/post/1774232/


All Articles