general array in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laurent Deniau

    #16
    Re: general array in C

    Cyn wrote:
    "Simon Biber" <news@ralmin.cc wrote in message
    news:453f269b_4 @news.peopletel ecom.com.au...
    >
    >>Jacob's solution, to use a union of different types, is a good one. But it
    >>would pay to consider what types you actually need to be able to store.
    >>The included types long long, double and long double are all at least 64
    >>bits and may be even more. The long double on my machine is 96 bits (12
    >>bytes), three times the size of a pointer to void or an int. That's
    >>tripling the size of the 'array'.
    >
    >
    Yes I like jacob's solution and I already made the same choice to not
    include 64 bits types (yet)
    For all the people recommending other languages, I work on embedded platform
    and C is the language we use. It's not like I can go around and say "hey
    lets change language because it fits the problem Im solving better"
    If you are on embedded platform, you probably care about memory usage
    and jacob's solution is not optimal (similar to what Perl do). See my
    other post.

    a+, ld.

    Comment

    • Keith Thompson

      #17
      Re: general array in C

      Laurent Deniau <laurent.deniau @cern.chwrites:
      Cyn wrote:
      >I want to create a general array structure which can hold all
      >types. Something like this:
      >struct ARRAY
      >{
      > void **array;
      > size_t size;
      >};
      >Work nice to store pointers to structures in it but what is a good
      >way to be able to both store pointers to structures and primitive
      >types in it?
      >
      see my post on "Extremly fast dynamic array implementation" in this group:
      >

      >
      The code is incomplete but I can provide full source code
      self-contained (actually it throws exceptions).
      Be sure to read that entire thread. The posted code is full of
      useless casts, it deliberately doesn't check for allocation errors,
      and it *isn't C*. (I think it depends on gcc-specific extensions.)

      And if your full source code "throws exceptions", it certainly isn't C.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • Michal Nazarewicz

        #18
        Re: general array in C

        "Cyn" <ser@n.tkwrites :
        I want to create a general array structure which can hold all types.
        Something like this:
        >
        struct ARRAY {
        void **array;
        size_t size;
        };
        >
        Work nice to store pointers to structures in it but what is a good
        way to be able to both store pointers to structures and primitive
        types in it?
        You've been given some solutions involving unions and pointers but as
        it was pointed they waste some memory. You may think of using
        something like (it requires C99):

        #v+
        struct array {
        size_t size;
        size_t element_size;
        char data[];
        };

        struct array *array_new(size _t size, size_t element_size) {
        struct array *arr = malloc(size * element_size + sizeof *arr);
        if (!arr) return 0;
        arr->size = size;
        arr->element_size = element_size;
        return arr;
        }

        struct void *array_get(stru ct array *arr, size_t pos) {
        return !arr || pos>=arr->size ? 0 : arr->data[pos * arr->element_size];
        }

        void array_set(struc t array *arr, size_t pos, void *element) {
        if (!arr || pos>=arr->size) return;
        memcpy(arr->data + pos * arr->element_size , element, arr->element_size );
        }
        #v-

        or (if using C99 is not an option), use:

        #v+
        struct array {
        size_t size;
        size_t element_size;
        char *data;
        };

        struct array *array_new(size _t size, size_t element_size) {
        struct array *arr = malloc(sizeof *arr);
        if (!array_init(ar r, size, element_size)) {
        free(arr);
        return 0;
        }
        return arr;
        }

        struct array *array_init(str uct array *arr, size_t size,
        size_t element_size) {
        char *data;
        if (!arr || ! (data = malloc(size * element_size))) return 0;
        arr->size = size;
        arr->element_size = element_size;
        arr->data = data;
        }

        void array_free(stru ct array *arr) {
        if (arr) {
        free(arr->data);
        free(arr);
        }
        }
        #v-

        or (another option not requiring array_free()):

        #v+
        struct array {
        size_t size;
        size_t element_size;
        char *data;
        };

        struct array *array_new(size _t size, size_t element_size) {
        struct array *arr = malloc(size * element_size + sizeof *arr);
        if (!arr) return 0;
        arr->size = size;
        arr->element_size = element_size;
        arr->data = arr + 1;
        return arr;
        }
        #v-

        I;m not sure if there would be no problems with alignment though.

        --
        Best regards, _ _
        .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
        ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
        ooo +--<mina86*tlen.pl >--<jid:mina86*jab ber.org>--ooO--(_)--Ooo--

        Comment

        • Laurent Deniau

          #19
          Re: general array in C

          Keith Thompson wrote:
          Laurent Deniau <laurent.deniau @cern.chwrites:
          >
          >>Cyn wrote:
          >>
          >>>I want to create a general array structure which can hold all
          >>>types. Something like this:
          >>>struct ARRAY
          >>>{
          >> void **array;
          >> size_t size;
          >>>};
          >>>Work nice to store pointers to structures in it but what is a good
          >>>way to be able to both store pointers to structures and primitive
          >>>types in it?
          >>
          >>see my post on "Extremly fast dynamic array implementation" in this group:
          >>
          >>http://groups.google.fr/group/comp.l...263e52c0850000
          >>
          >>The code is incomplete but I can provide full source code
          >>self-contained (actually it throws exceptions).
          >
          >
          Be sure to read that entire thread.
          I did.
          The posted code is full of
          useless casts,
          Could you show me the useless casts in the posted code? I do not say
          that the code is perfect, but I do not see useless casts ((void*) casts
          are there for information)
          it deliberately doesn't check for allocation errors,
          Could you show me what allow you to say that?
          and it *isn't C*. (I think it depends on gcc-specific extensions.)
          This is pure C89 code (inline vanishes if OOC_ISO_C < OOC_ISO_C99).

          May I suggest you to read again the code?
          And if your full source code "throws exceptions", it certainly isn't C.
          This is pure C89 code, it compiles without warning with the following
          gcc 4.1.1 options:

          -std=c89 -pedantic -Wall -W -Wstrict-prototypes -Wmissing-prototypes
          -Wmissing-declarations -Wchar-subscripts -Wformat-nonliteral
          -Wcast-align -Wpointer-arith -Wbad-function-cast -Winline -Wcast-qual
          -Wshadow -Wwrite-strings -Wfloat-equal -Wconversion -Wno-conversion
          -O3 -fno-ident -floop-optimize2 -fgcse-sm -fgcse-las

          But I as said, the posted code is part of a framework which amongst of
          other, implements exceptions in C. I can make it free from this
          dependance on request which will of course make new and alloc return
          NULL instead of throwing an exception, and make get and set will use
          assert instead of throwing an exception. This is the price to pay for
          standalone code.

          a+, ld.

          Comment

          • Laurent Deniau

            #20
            Re: general array in C

            Michal Nazarewicz wrote:
            "Cyn" <ser@n.tkwrites :
            >
            >>I want to create a general array structure which can hold all types.
            #v+
            struct array {
            size_t size;
            size_t element_size;
            char *data;
            };
            >
            struct array *array_new(size _t size, size_t element_size) {
            struct array *arr = malloc(size * element_size + sizeof *arr);
            if (!arr) return 0;
            arr->size = size;
            arr->element_size = element_size;
            arr->data = arr + 1;
            return arr;
            }
            #v-
            >
            I;m not sure if there would be no problems with alignment though.
            There will (e.g. you cannot store double in your array). See my post
            which does exactly this by providing a type specific interface
            ("template" code) while implementation is generic but requires hidden
            information. This solution is as same access semantic and efficiency as
            C arrays (e.g. a[i]), has no problem of alignment, and minimize memory
            usage.

            a+, ld.

            Comment

            • Keith Thompson

              #21
              Re: general array in C

              Laurent Deniau <laurent.deniau @cern.chwrites:
              Keith Thompson wrote:
              >Laurent Deniau <laurent.deniau @cern.chwrites:
              [...]
              >>>see my post on "Extremly fast dynamic array implementation" in this group:
              >>>
              >>>http://groups.google.fr/group/comp.l...263e52c0850000
              >>>
              >>>The code is incomplete but I can provide full source code
              >>>self-contained (actually it throws exceptions).
              >Be sure to read that entire thread.
              >
              I did.
              >
              > The posted code is full of
              >useless casts,
              >
              Could you show me the useless casts in the posted code? I do not say
              that the code is perfect, but I do not see useless casts ((void*)
              casts are there for information)
              >
              >it deliberately doesn't check for allocation errors,
              >
              Could you show me what allow you to say that?
              >
              >and it *isn't C*. (I think it depends on gcc-specific extensions.)
              >
              This is pure C89 code (inline vanishes if OOC_ISO_C < OOC_ISO_C99).
              >
              May I suggest you to read again the code?
              My apologies. I was referring to the code in the article that started
              the thread:

              <http://groups.google.c om/group/comp.lang.c/msg/9c9c34004ae180b 5>

              not to the code that you posted later:

              <http://groups.google.c om/group/comp.lang.c/msg/0b263e52c085000 0>

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              Working...