Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My first question is how do you calculate len? Is it just strlen or does it include room for the \0 terminator? I think you may be overshooting your allocation in your strcpy. Bad behavior will tend to happen on word boundaries and appear random. Also, check to make sure that your source strings are null terminated. If you made a mistake on the read side and didn't terminate them. Then strcpy may be randomly overwriting things. </p> <pre><code> *(lines + slot) = (char *) malloc(len * sizeof(char)); if(*(lines + slot) == NULL) exit(EXIT_FAILURE); strcpy(*(lines+slot),s); </code></pre> <p>Perhaps try:</p> <pre><code> lines[slot] = (char *) malloc((len + 1) * sizeof(char)); if(lines[slot] == NULL) exit(EXIT_FAILURE); if(strlen(s) &lt;= len){ strcpy(lines[slot],s); } else{ /* do something else... */ } </code></pre> <p>In terms of general form, I'd also encourage you to make a few stylistic changes to make the whole thing a bit more readable, easier to follow and resistant to errors.</p> <p>Pointer arithmetic is valid and fun, but I think your intent is a little more clear if you use the array form like: </p> <pre><code>free(lines[slot]); lines[slot] = NULL; </code></pre> <p>instead of</p> <pre><code>free(*(lines+slot)); *(lines + slot) = NULL; </code></pre> <p>I'd also encourage you to use fewer statics. It's easy enough to through them in a data structure and pass them around into your accessors and mutators. It becomes much more clear where the action is happening prevents you from doing things like:</p> <pre><code>static int numlines = 0; void insert_line(char *s, int len){ int numlines = 5; </code></pre> <p>where you can introduce scoping issues that are just miserable to debug.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload