Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4027050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:02:54+00:00 2026-05-20T11:02:54+00:00

I was having some weird Glib behavior, searched the internet a little and found

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:02:55+00:00Added an answer on May 20, 2026 at 11:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some weird results from this perl code - I need to delete
I'm new to Ruby, so I'm having some trouble understanding this weird exception problem
Um, this might sound a bit weird. We were having some problems with a
Alright, I have some weird behavior and this question goes to the people out
I'm having some weird issues improving a bookmarklet. I took this example from here
I'm having some kind of weird problem with my CSS. I refactored some code
I have InApp purchasing setup in my app. I am having some weird behavior
I'm having some problems with a weird HTTP status code in MSIE8. I send
I am having a weird error when using Omniauth. This appears to have little
I am having some weird performance issues with evalRandIO. Here's the offending code: import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.