general array in C

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

    #1

    general array in C

    Hi,
    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?


  • jacob navia

    #2
    Re: general array in C

    Cyn wrote:
    Hi,
    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?
    >
    >
    typedef union {
    void *Vptr;
    int Int;
    short Short;
    char Char;
    long Long;
    long long LLong;
    double Double;
    long double LDouble;
    float Float;
    } CELL;

    struct ARRAY {
    CELL *array;
    size_t size;
    };

    struct ARRAY *newArray(size_ t siz)
    {
    // Allocates and returns an ARRAY
    // ...
    }

    int main(void)
    {
    ARRAY *a;

    a = newArray(1024);
    a.array[23].Float = 2.13f;
    a.array[24].Vptr = malloc(267);
    a.array[25].LLong = 533777899LL;
    a.array[26].Char = 'F';
    a.array[27].Vptr = "This is an example string";
    }

    I would use a type field to avoid mistakes, storing which
    type is stored in tha array by changing:
    struct ARRAY {
    CELL *array;
    char *CellTypes;
    size_t size;
    };

    #define VPTR 1
    #define INT 2
    #define SHORT 3
    .... etc

    and you store the type of each cell in the adjacent array of characters.


    jacob

    Comment

    • Ancient_Hacker

      #3
      Re: general array in C


      Cyn wrote:
      Hi,
      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're trying to re-invent the C++ object. If you want to do much of
      this, it's best to let the C++ compiler and run-time library worry
      about all this.

      Some other fields you may want to include: the size of each element, a
      code for the type of element, the number of dimensions, maybe a link to
      the next object (for garbage collection)

      Comment

      • jacob navia

        #4
        Re: general array in C

        Ancient_Hacker wrote:
        Cyn wrote:
        >
        >>Hi,
        >>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're trying to re-invent the C++ object. If you want to do much of
        this, it's best to let the C++ compiler and run-time library worry
        about all this.
        >
        ????

        See my post above. Why do you need a "C++ object" to do that?

        can you explain?

        Thanks
        Some other fields you may want to include: the size of each element, a
        code for the type of element, the number of dimensions, maybe a link to
        the next object (for garbage collection)
        >

        Comment

        • Ancient_Hacker

          #5
          Re: general array in C


          jacob navia wrote:
          See my post above. Why do you need a "C++ object" to do that?

          You don't. And you can build your own jet plane with enough Electrolux
          vacuum cleaners, aluminum cat food cans, "crazy glue", and duct tape.
          But many people find it easier to just go and buy one off the shelf.

          It's just that C++ is really good at doing all the error-prone busywork
          of creating objects, and it lets you define operators, often overused,
          on those objects.

          You can do similar things in C, but the results are rarely pretty,
          concise, and easy to use.

          Not to polish the C++ apple too much, in many ways it's a horrible
          design, but it does do a few things a whole lot better than most of us
          can cobble up out of odd bits.

          Comment

          • Frederick Gotham

            #6
            Re: general array in C

            Ancient_Hacker posted:
            >
            Cyn wrote:
            >Hi,
            >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're trying to re-invent the C++ object.

            Hmm... I don't see how it's anything like a class object in C++. If anything,
            I'd say he's trying to implement the Variant type which exists in Visual
            Basic.

            --

            Frederick Gotham

            Comment

            • pete

              #7
              Re: general array in C

              Cyn wrote:
              >
              Hi,
              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?
              With a generic list node:
              struct list_node {
              struct list_node *next;
              void *data;
              };
              you can have lists of any data format.

              --
              pete

              Comment

              • jacob navia

                #8
                Re: general array in C

                pete wrote:
                Cyn wrote:
                >
                >>Hi,
                >>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?
                >
                >
                With a generic list node:
                struct list_node {
                struct list_node *next;
                void *data;
                };
                you can have lists of any data format.
                >
                Yes but if you want to store a character, you waste sizeof(void *) bytes
                in a pointer to... 1 byte.

                Isn't a union (as I indicated in my answer) a better solution?

                Comment

                • Bart

                  #9
                  Re: general array in C

                  Cyn wrote:
                  Hi,
                  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?
                  If you need dynamic typing why are you using C? A dynamically typed
                  language such as Scheme or Python would be a better choice for such
                  things.

                  Regards,
                  Bart.

                  Comment

                  • Bart

                    #10
                    Re: general array in C

                    Ancient_Hacker wrote:
                    Not to polish the C++ apple too much, in many ways it's a horrible
                    design, but it does do a few things a whole lot better than most of us
                    can cobble up out of odd bits.
                    But this case is not that much better suited for C++. See my other
                    reply for better alternatives.

                    Regards,
                    Bart.

                    Comment

                    • Simon Biber

                      #11
                      Re: general array in C

                      pete wrote:
                      Cyn wrote:
                      >Hi,
                      >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?
                      >
                      With a generic list node:
                      struct list_node {
                      struct list_node *next;
                      void *data;
                      };
                      you can have lists of any data format.
                      But if you want to use that list node to store a list of ints, you still
                      have to store the ints elsewhere and put pointers to them into the list.
                      That's a waste of memory compared with being able to put the ints
                      directly into the list_node structure.

                      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'.

                      --
                      Simon.

                      Comment

                      • Ancient_Hacker

                        #12
                        Re: general array in C


                        Frederick Gotham wrote:
                        Hmm... I don't see how it's anything like a class object in C++.
                        You're right, it is not a simple class object. But if you think it
                        through, he's going to need ways of operating on gamut of types, and C
                        doesnt provide much help there, but with C++ operator definitions
                        there's a way.

                        Comment

                        • Frederick Gotham

                          #13
                          Re: general array in C

                          Ancient_Hacker posted:
                          >Hmm... I don't see how it's anything like a class object in C++.
                          >
                          You're right, it is not a simple class object. But if you think it
                          through, he's going to need ways of operating on gamut of types, and C
                          doesnt provide much help there, but with C++ operator definitions
                          there's a way.

                          Here's what the OP said:

                          I want to create a general array structure which can hold all types.
                          Something like this:

                          struct ARRAY
                          {
                          void **array;
                          size_t size;
                          };

                          The question was poor formed, as it does not coincide with the sample
                          snippet. All the sample snippet does is store a pointer and a length in a
                          struct!

                          This is trivial:

                          struct PtrPlusLenght {
                          void const *p;
                          size_t len;
                          };

                          Wrapping an array within a struct is a different concept altogether. In
                          C++, it might be done something like:

                          template<class T,size_t len>
                          struct ArrayWrapper {
                          T arr[len];
                          };

                          , while in C, it might be something like:

                          #define DEFINE_TYPE(T,l en)\
                          struct ArrayWrapper_## T##_##len {\
                          T arr[len];
                          };

                          (Of course, you'll have problems in C if the type is something like char
                          (*p)(int) ).

                          --

                          Frederick Gotham

                          Comment

                          • Laurent Deniau

                            #14
                            Re: general array in C

                            Cyn wrote:
                            Hi,
                            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).

                            a+, ld.

                            Comment

                            • Cyn

                              #15
                              Re: general array in C


                              "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"


                              Comment

                              Working...