The user nemequ sent me in the right direction. I found out that in another function call, I was releasing the data GArray using the free(data); call, which is not good. GArrays are supposed to be released using g_array_free(data, TRUE); That was my problem. The memory got corrupted.
glib g_array_append_val() acting strange
Question
I have this very simple piece of code. I declare a glib array of doubles in C. Then I fill it using the g_array_append_val() function and, lastly, I print out the array content. The printed out version doesn't show the array items to be 0.0, 1.0, 2.0,..9.0 as I would expect. What could be wrong with this? Any ideas?
GArray *data;
data = g_array_new(FALSE, FALSE, sizeof(double));
int i;
for(i = 0; i < 10; i++) {
double d = (double)i;
g_array_append_val(data, d);
}
for(i = 0; i < 10; i++)
printf("%f ", g_array_index(data, double, i));
free(data);
The output is here:
0.000000 1.000000 2.000000 3.000000 4.000000 5.000000 0.000000 0.000000 0.000000 0.000000
I even looked up the test suite for the glib library and saw them doing it this exact way. This baffles me.
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow