Loading ...
Sorry, an error occurred while loading the content.

Re: Dynamic arrays

Expand Messages
  • martweb2000
    Can we do something similar in pure C without using that lib? Some simple example code? ... have the ... know ... very ... more,
    Message 1 of 5 , Oct 23, 2006
    • 0 Attachment
      Can we do something similar in pure C without using that lib? Some
      simple example code?

      --- In autotrace@yahoogroups.com, Masatake YAMATO <jet@...> wrote:
      >
      > Hi,
      >
      > > I am looking for a better way creating dynamic arrays in C. I
      have the
      > > following problem. First I create an array with the size 1 then I
      > > resized it to size 2 then to size 3 ... always by one as I do not
      know
      > > the final size. For size I use realloc, but doing it such way is
      very
      > > slow. Any better idea in C?
      >
      > If you allocated n byte with realloc last time and you need n+1 or
      more,
      > allocate 2*n with realloc.
      >
      > realloc already does so?
      > STL does something more clever?
      >
      > Masatake
      >
    • Masatake YAMATO
      ... I can do nothing with pure C :-P Here is the code from glib/glib/garray.c: static void g_array_maybe_expand (GRealArray *array, gint len) { guint
      Message 2 of 5 , Oct 23, 2006
      • 0 Attachment
        > Can we do something similar in pure C without using that lib? Some
        > simple example code?

        I can do nothing with pure C :-P

        Here is the code from glib/glib/garray.c:
        static void
        g_array_maybe_expand (GRealArray *array,
        gint len)
        {
        guint want_alloc = g_array_elt_len (array, array->len + len +
        array->zero_terminated);

        if (want_alloc > array->alloc)
        {
        want_alloc = g_nearest_pow (want_alloc);
        want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);

        array->data = g_realloc (array->data, want_alloc);

        #ifdef ENABLE_GC_FRIENDLY
        memset (array->data + array->alloc, 0, want_alloc - array->alloc);
        #endif /* ENABLE_GC_FRIENDLY */

        array->alloc = want_alloc;
        }
        }
      • martweb2000
        What is faster - using the glib or stl? ... Some
        Message 3 of 5 , Oct 23, 2006
        • 0 Attachment
          What is faster - using the glib or stl?

          --- In autotrace@yahoogroups.com, Masatake YAMATO <jet@...> wrote:
          >
          > > Can we do something similar in pure C without using that lib?
          Some
          > > simple example code?
          >
          > I can do nothing with pure C :-P
          >
          > Here is the code from glib/glib/garray.c:
          > static void
          > g_array_maybe_expand (GRealArray *array,
          > gint len)
          > {
          > guint want_alloc = g_array_elt_len (array, array->len + len +
          > array->zero_terminated);
          >
          > if (want_alloc > array->alloc)
          > {
          > want_alloc = g_nearest_pow (want_alloc);
          > want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
          >
          > array->data = g_realloc (array->data, want_alloc);
          >
          > #ifdef ENABLE_GC_FRIENDLY
          > memset (array->data + array->alloc, 0, want_alloc - array-
          >alloc);
          > #endif /* ENABLE_GC_FRIENDLY */
          >
          > array->alloc = want_alloc;
          > }
          > }
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.