Note that there are some explanatory texts on larger screens.

plurals
  1. POC: using pointer as string: unpredictable behavior
    text
    copied!<p>I'm writing a C program to find the longest line in the user's input and print the line's length and the line itself. It succeeds at counting the characters but unpredictably fails at storing the line itself. Maybe I'm misunderstanding C's memory management and someone can correct me.</p> <p><strong>EDIT: followup question:</strong> I understand now that the blocks following the <code>dummy</code> char are unallocated and thus open range for the computer to do anything with them, but then why does the storage of some chars still work? In the second example I mention, the program stores characters in the 'unallocated' blocks even though it 'shouldn't'. Why?</p> <p>Variables:</p> <ul> <li><code>getchar()</code> is stored in <code>c</code> every time i <code>getchar()</code></li> <li><code>i</code> is the length (so far) of the current line i'm <code>getchar()</code>ing from</li> <li><code>longest_i</code> is the length of the longest line so far</li> <li><code>twostr</code> points to the beginning of the first of two strings: the first for the current line, the second for the longest line so far. When a line is discovered to be the longest, it is copied into the second string. If a future line is even longer, it overrides some of the second string but that's OK because I won't use it anymore -- the second string will now begin at a location farther to the right.</li> <li><code>dummy</code> gives <code>twostr</code> a place to point to</li> </ul> <p>This is how I visualize the memory used by the program's variables:</p> <pre><code> 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |\n| 7|11|15|c |u |r |r |e |n |t |\0|e |s |t |\0|p |r |e |v |l |o |n |g |e |s |t |\0| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ </code></pre> <p>true statements:</p> <pre><code>&amp;c == 11 &amp;i == 12 &amp;longest_i == 13 &amp;twostr = 14 &amp;dummy = 15 </code></pre> <p>program:</p> <pre><code>#include &lt;stdio.h&gt; int main() { char c = '\0'; int i, longest_i; char *twostr; longest_i = i = 0; char dummy = '\0'; twostr = &amp;dummy; while ((c=getchar()) != EOF) { if (c != '\n') { *(twostr+i) = c; i++; } else { *(twostr+i) = '\0'; if (i &gt; longest_i) { longest_i = i; for (i=0; (c=*(twostr+i)) != '\0'; ++i) *(twostr+longest_i+1+i) = c; } i = 0; } } printf("length is %d\n", longest_i); for (i=0; (c=*(twostr+longest_i+1+i)) != '\0'; ++i) putchar(c); return 0; } </code></pre> <p>From <code>*(twostr+longest_i+1))</code> until <code>'\0'</code> is unpredictable. Examples:</p> <p>input:</p> <pre><code>longer line line </code></pre> <p>output:</p> <pre><code>length is 11 @ </code></pre> <p>input:</p> <pre><code>this is a line this is a longer line shorter line </code></pre> <p>output:</p> <pre><code>length is 21 this is a longer lineÔÿ" </code></pre>
 

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