GArray 未按预期在初始化时清除
我有一些奇怪的 Glib 行为,在互联网上搜索了一下,发现 这个 Glib 教程,具体的第二个代码块:
//ex-garray-2.c
#include <glib.h>
#include <stdio.h> // I added this to make it compile
int main(int argc, char** argv) {
GArray* a = g_array_sized_new(TRUE, TRUE, sizeof(int), 16);
printf("Array preallocation is hidden, so array size == %d\n", a->len);
printf("Array was init'd to zeros, so 3rd item is = %d\n",
g_array_index(a, int, 2));
g_array_free(a, FALSE);
// I removed some code here
return 0;
}
所以,预期的结果应该是,
Array preallocation is hidden, so array size == 0
Array was init'd to zeros, so 3rd item is = 0
但我确实得到了
Array preallocation is hidden, so array size == 0
Array was init'd to zeros, so 3rd item is = 901959560
我正在使用 gcc 4.5.3、glibc 2.13 和 glib 2.26 运行 Gentoo Linux ~amd64(64 位,测试) .1.我使用 gcc $(pkg-config --cflags --libs glib-2.0) -o ex-garray-2 ex-garray-2.c 编译了程序
知道为什么我会得到观察到的行为吗而不是预期的?
I was having some weird Glib behavior, searched the internet a little and found this Glib tutorial, the second code block to be specific:
//ex-garray-2.c
#include <glib.h>
#include <stdio.h> // I added this to make it compile
int main(int argc, char** argv) {
GArray* a = g_array_sized_new(TRUE, TRUE, sizeof(int), 16);
printf("Array preallocation is hidden, so array size == %d\n", a->len);
printf("Array was init'd to zeros, so 3rd item is = %d\n",
g_array_index(a, int, 2));
g_array_free(a, FALSE);
// I removed some code here
return 0;
}
So, the expected result should be
Array preallocation is hidden, so array size == 0
Array was init'd to zeros, so 3rd item is = 0
but I do get
Array preallocation is hidden, so array size == 0
Array was init'd to zeros, so 3rd item is = 901959560
I am running Gentoo Linux ~amd64 (64bit, testing) with gcc 4.5.3, glibc 2.13 and glib 2.26.1. I compiled the program using gcc $(pkg-config --cflags --libs glib-2.0) -o ex-garray-2 ex-garray-2.c
Any idea why I get the observed behavior and not the expected one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 GLib 不太熟悉,但我发现它的文档非常具有误导性:
g_array_sized_new
中的clear_
标志实际上定义了g_array_set_size
的行为方式,即是否在设置数组大小时将位设置为零。事实上,GLib 的 文档 说因此,您尝试访问索引大于数组大小的元素。尝试先使用
g_array_set_size (a,16)
设置数组的大小,然后只有您可以访问第三项。I am not very familiar with GLib, but I found its documentation very misleading: the flag
clear_
ing_array_sized_new
actually defines howg_array_set_size
behaves, i.e., whether it sets the bits to zero or not upon setting the size of the array. Indeed, the documentation of GLib saysTherefore, you are trying to access an element whose index is larger than the size of the array. Try to set the size of the array using
g_array_set_size (a,16)
first, and then only you can access the 3rd item.IBM 文档不正确。您必须像这样取消引用 g_array_index() :
请参阅 http: //developer.gnome.org/glib/2.28/glib-Arrays.html#g-array-index
The IBM documentation isn't correct. You have to dereference g_array_index() like this:
See http://developer.gnome.org/glib/2.28/glib-Arrays.html#g-array-index