GArray 未按预期在初始化时清除

发布于 2024-10-19 06:26:49 字数 1122 浏览 3 评论 0原文

我有一些奇怪的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2024-10-26 06:26:49

我对 GLib 不太熟悉,但我发现它的文档非常具有误导性:g_array_sized_new 中的 clear_ 标志实际上定义了 g_array_set_size 的行为方式,即是否在设置数组大小时将位设置为零。事实上,GLib 的 文档

g_array_sized_new 创建一个新的 GArray,其中预分配了 returned_size 元素且引用计数为 1。如果要向数组添加许多元素,这可以避免频繁的重新分配。 但请注意,数组的大小仍然为 0。

因此,您尝试访问索引大于数组大小的元素。尝试先使用 g_array_set_size (a,16) 设置数组的大小,然后只有您可以访问第三项。

I am not very familiar with GLib, but I found its documentation very misleading: the flag clear_ in g_array_sized_new actually defines how g_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 says

g_array_sized_new creates a new GArray with reserved_size elements preallocated and a reference count of 1. This avoids frequent reallocation, if you are going to add many elements to the array. Note however that the size of the array is still 0.

Therefore, 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.

执妄 2024-10-26 06:26:49

IBM 文档不正确。您必须像这样取消引用 g_array_index() :

printf("%d\n", &g_array_index(array, type, 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:

printf("%d\n", &g_array_index(array, type, index));

See http://developer.gnome.org/glib/2.28/glib-Arrays.html#g-array-index

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文