Note that there are some explanatory texts on larger screens.

plurals
  1. POPointer value changed in XML parse start handler
    text
    copied!<p>I have an int pointer that for some reason has its value being modified as soon as it is passed out of the main. This file is using the expat XML_Parse command. Here is the relevant code:</p> <p>The struct that I set the values in:</p> <pre><code>struct song_data { float *freq_array; int *freq_array_count; int *freq_duration_array; }; </code></pre> <p>Part of (most of) main:</p> <pre><code>int main(int argc, char **argv) { const int BUFFER_SIZE = 8192; char buffer[BUFFER_SIZE]; struct song_data * wav = (struct song_data *)malloc(sizeof(struct song_data)); *wav-&gt;freq_array_count=0; XML_Parser parser = XML_ParserCreate(NULL); //create parser XML_SetElementHandler(parser, start, end); XML_SetUserData(parser, &amp;wav); FILE *xml_file = fopen(argv[1], "r"); int done = 0; while (!done) { int length = fread(buffer, sizeof(char), BUFFER_SIZE, xml_file); done = feof(xml_file); XML_Parse(parser, buffer, length, done); } </code></pre> <p>And the start handler:</p> <pre><code>void start(void *data, const char *tag, const char **attributes) { struct song_data *song_data = (struct song_data *) data; printf("in start, freq_array_count=%d\n,*song_data-&gt;freq_array_count); // rest of code } </code></pre> <p>The printf in the start handler prints some weird value like -7194902 or something (the server I was working on is currently down). </p> <p>Another approach that I have tried (and would prefer, if I can figure out how to get it working) is having the <code>int *freq_array_count</code> just be an int (<code>int freq_array_count</code>)... however this also gets a weird value immediately the first time the start handler is called (something above 100,000,000)</p> <p>Why? I feel as if the int value, in the approach taken here, should not be modified because it is pointing to a part of memory I explicitly asked for.</p> <p>In the other approach it shouldn't be modified either because the int belongs to the struct on the heap, and thus should not be modified. </p> <p>Why is this happening?</p>
 

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