문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top