This is the mail archive of the gcc-prs@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]

c/132: Re: Feature-request: Possibility of transparent unions in structs



>Number:         132
>Category:       c
>Synopsis:       Feature-request: Possibility of transparent unions in structs
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          analyzed
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sat Mar 25 01:06:01 PST 2000
>Closed-Date:
>Last-Modified:
>Originator:     Byron Stanoszek <gandalf@winds.org>
>Release:        2.95.2
>Organization:
>Environment:
>Description:
 Date: Fri, 24 Mar 2000 22:56:44 -0500 (EST)
 Original-Message-ID: <Pine.LNX.4.21.0003242246090.16101-100000@winds.org>

 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>

>How-To-Repeat:
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:

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