public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Feature-request: Possibility of transparent unions in structs
@ 2000-03-24 20:08 Byron Stanoszek
  2000-03-25  9:12 ` Mumit Khan
  2000-03-27  9:36 ` Joe Buck
  0 siblings, 2 replies; 3+ messages in thread
From: Byron Stanoszek @ 2000-03-24 20:08 UTC (permalink / raw)
  To: gcc

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>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Feature-request: Possibility of transparent unions in structs
  2000-03-24 20:08 Feature-request: Possibility of transparent unions in structs Byron Stanoszek
@ 2000-03-25  9:12 ` Mumit Khan
  2000-03-27  9:36 ` Joe Buck
  1 sibling, 0 replies; 3+ messages in thread
From: Mumit Khan @ 2000-03-25  9:12 UTC (permalink / raw)
  To: Byron Stanoszek; +Cc: gcc

On Fri, 24 Mar 2000, Byron Stanoszek wrote:

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

[ ... ]

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

Yes. Support for anonymous aggregates (structs and unions) in C was added
back in October, 1999 and will show up in the next major release.

Regards,
Mumit


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Feature-request: Possibility of transparent unions in structs
  2000-03-24 20:08 Feature-request: Possibility of transparent unions in structs Byron Stanoszek
  2000-03-25  9:12 ` Mumit Khan
@ 2000-03-27  9:36 ` Joe Buck
  1 sibling, 0 replies; 3+ messages in thread
From: Joe Buck @ 2000-03-27  9:36 UTC (permalink / raw)
  To: Byron Stanoszek; +Cc: gcc

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

In C++, there is.  It's called anonymous unions.  For your example, you
would write

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

and then, given
	foo *bar;

you can write bar->id, bar->data, bar->string, bar->type.

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

It does not exist as a C extension.  I'm not convinced that it is a good
idea to move C++ features into the C front end as extensions on an ad hoc
basis, though I suppose some folks think we set a precedent when C++
comments were allowed.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2000-03-27  9:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-24 20:08 Feature-request: Possibility of transparent unions in structs Byron Stanoszek
2000-03-25  9:12 ` Mumit Khan
2000-03-27  9:36 ` Joe Buck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).