This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Feature-request: Possibility of transparent unions in structs


I have a large piece of code that uses the members of certain structures
conditionally depending on other members of the structure. Furthermore, since
only some of the members of the struct are used at some times, I feel I could
save memory by making those members into a union. The problem is, I don't want
to change those struct members in 60,000 lines of code.

The question: Is there a way to make the union identifier transparent to the
structure, such that the members of the union can be referenced from the
context of the structure?

Example:

Changing the original structure:

struct foo {
  int type;    // Type= 0:use id, 1=use data, 2=use string
  int id;
  int *data;
  char *string;
};

to this to decrease size of structure in memory:

struct foo {
  int type;
  union ident {
    int id;
    int *data;
    char *string;
  };
};

BUT with the ability to retain the original calling method from a function:

void bar() {
  struct foo *ptr;

  switch(ptr->type)
  {
    case 0:
      id_function(ptr->id);
      break;
    case 1:
      data_function(ptr->data);
      break;
    case 2:
      string_function(ptr->string);
      break;
  }
}

I looked up the info files and found something close-- transparent_union
attribute, but it (assumingly) only works with function calls and not from
within a structure, as per documentation states. What I'd really like to do is
limit my memory usage without having to rename each portion of code to
something that looks like ptr->ident.id, etc.

Can anyone tell me if this feature exists currently in GCC (as a C extension),
or if possible, can be added to gcc 2.96 / 3.0?

Thank you!

	-- Byron Stanoszek <gandalf@winds.org>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]