Cloning structs

From: Patrick Dughi (dughi@imaxx.net)
Date: 03/27/99


(Note: Rather long, and ends with a request for technical info - may not
be terribly interesting.)

        In truth, this is more about programming in general than directly
applying to circle, but it's something that I thought about a great deal,
and would be terribly helpful for building, as well as a myraid of other
neat things..

        Basically, I want to clone a struct.  Say I want to make an exact
copy of something to edit.  Currently one would have to make a seperate
function for each structure type; they'd alloc the space, and copy
relevant variables, creating each new string/struct within as they go,
eventually making it all...

        Sure, you can do that for rooms, objects, and pc structs and
you've probably got around 95% of your functionality there - but I
envision a function that is more, well, automatic.  If I add something to
a struct, then I have to go through these cloning functions and add each
seperate thing in turn.  And if I want to clone a struct that hasn't got
its own function, well, i've no choice but to write it up..

        Example:

struct another_struct = {
  int var3;
  char *string_two;
};

struct bogus_struct = {
  int variable1;
  int variable2;
  char *a_string;
  struct another_struct *meep;
  struct bogus_struct *next;
};

struct bogus_struct example = { 1,2,"Spiffy",{1,"forty-seven"},NULL};

struct bogus_struct *clone_struct;

  clone_struct = clone((struct bogus_struct *) example);

  and poof, the values of clone_struct are set equal to those in example..
strings, other structs, etc.  Of course, with lists, we wouldn't need (or
want to..for that matter) iterate down the list, but i'm sure those can be
examined - disallow if it points to another of the same type, or some
other testable circumstance.

        I know what I want, but my question is how would one go about
doing this?  I'm curious if there's some way to tell the difference
between a fixed size string (like string[20]) instead of a char pointer
which requires memory to be explicitly allocated. To further go on, how to
tell a struct pointer within a struct.. The memory itself is rather
trivial to copy once you know the type of structure involved, or rather..
just the size (which you may be able to get from the struct to be cloned -
but since things like pointers may have a variable amount of size
associated with them... maybe this is a bad idea).  I'm curious as to how
to figure out when new memory has to be allocated, and how I can determine
that a structure needs additional help (has components which need memory
allocated).
        Any help on this, or is this too advanced for the forum?

                                                        PjD


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST