Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!spool.mu.edu!uunet!cimshop!davidm From: cimshop!davidm@uunet.UU.NET (David S. Masterson) Newsgroups: comp.lang.c++ Subject: Re: structures with variable length elements. Message-ID: Date: 21 May 91 20:03:40 GMT Article-I.D.: uunet.CIMSHOP!DAVIDM.91May21130340 References: <1991May18.010928.16619@cshl.org> <736@taumet.com> Sender: davidm@cimshop.UUCP Organization: Consilium Inc., Mountain View, California Lines: 48 In-reply-to: steve@taumet.com's message of 20 May 91 17:23:36 GMT >>>>> On 20 May 91 17:23:36 GMT, steve@taumet.com (Stephen Clamage) said: Stephen> Another way, which also works in C++, is to make 'i' a pointer, then Stephen> allocate an array for 'i' to point to whenever you create an instance Stephen> of the struct. Then the order of the fields doesn't matter. In C, Stephen> this technique requires some careful bookkeeping, but in C++ it all Stephen> goes in the constructor and destructor, and you don't have to worry Stephen> about it in the rest of the code. Stephen> class var_int { Stephen> ... Stephen> int *ip; Stephen> int array_size; // size of the ip array Stephen> public: Stephen> var_int(int sz); // constructor allocates array of sz ints Stephen> ~var_int(); // destructor deallocates the array Stephen> size(); // return number of elements Stephen> int& operator[](int index); // access array element Stephen> }; Hmmm, you're beginning to hit a problem that is close to home (and one I'm still having trouble with). In my system, we are moving events in and out of the system. Events come in two flavors, fixed-length and variable-length (variable-length is often used for shipping arbitrary lists of information). So Events might look like: class Event { ... }; class FixedEvent : public Event { ... }; class VarEvent : public Event { ... }; The question I have is what should VarEvent look like internally such that a pointer to it could be handed off to: boolean EventShipper(Event*); // assuming Event knows how big it is. and EventShipper could just copy it into a "mailbox" for shipment. Obviously, if this is just a "straight" copy, then Events really can't have virtual capabilities (a problem in the current design). Given that, though, a natural way of arranging this is as above and memcpy() the Event from place to place. The problem, though, is that the VarEvent needs the ability to grow to some given size. What is the best way of handling this? -- ==================================================================== David Masterson Consilium, Inc. (415) 691-6311 640 Clyde Ct. uunet!cimshop!davidm Mtn. View, CA 94043 ==================================================================== "If someone thinks they know what I said, then I didn't say it!"