Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Solution</strong></p> <p>Change this line:</p> <pre><code>xml_header[xml_level] = strtok (buffer, " "); </code></pre> <p>to</p> <pre><code>xml_header[xml_level] = strdup (strtok(buffer, " ")); </code></pre> <p>And remember to free xml_headers when your program exit. </p> <p><strong>[UPDATE]</strong> Of course, you also need to check possible corner cases such as <code>strtok</code> returns <code>NULL</code>, etc...</p> <p><strong>Explanation</strong></p> <p><code>strtok</code> won't allocate extra storage for the returned tokens. My personal suspect is that it will subsititute the delimiters with <code>\0</code> <strong>in place</strong> and return you the pointer to the start of next token every time.</p> <p>Note that in your code, you allocate 256 bytes of buffer both in <code>xmlopen</code> and <code>xmlwrite</code> at the beginning, and recall that this buffer will be allocated in stack. So in your calls to <code>xmlopen</code> or <code>xmlwrite</code>, <code>buffer</code> will actually point to the same address (you can print its value to verify this <code>printf("buffer is %p\n", buffer)</code>, it's <code>0xbff2481c</code> on my machine).</p> <p>First, you call <code>xmlopen("Hello World")</code>, and <code>xml_header[0]</code> will point to <code>"Hello"</code>, which is also the start of <code>buffer</code>. Then you call <code>xmlopen("Foo Bar")</code>, and <code>xml_header[1]</code> will point to <code>"Foo"</code>, which also the start of <code>buffer</code>. Then you call <code>xmlwrite("Portato")</code> and <code>xmlwrite("Sentence longer than the other ones")</code>. Note that at this point, <code>xml_header[1]</code> still point to the start of <code>buffer</code>, which now is <code>"Sentence longer than the other ones"</code>. So when you call <code>xmlclose()</code>, it will print out that sentence instead of your expected token, which is overwrite by your later buffer.</p> <p>Interestingly, if you allocate different buffer size in <code>xmlopen</code> and <code>xmlwrite</code>, say 256 bytes for <code>xmlopen</code> and 128 bytes for <code>xmlwrite</code>, then you will see that <code>xmlclose</code> will print out some mess codes which is not readable. </p> <p>You can verify all these by inspect (e.g., print) the values of <code>xml_header[0], xml_header[1]</code> and <code>buffer</code>.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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