Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!umnd-cs!umn-cs!herndon From: herndon@umn-cs.UUCP (Robert Herndon) Newsgroups: comp.lang.c Subject: Portable Memory Allocation Message-ID: <1645@umn-cs.UUCP> Date: Mon, 8-Jun-87 23:45:23 EDT Article-I.D.: umn-cs.1645 Posted: Mon Jun 8 23:45:23 1987 Date-Received: Thu, 11-Jun-87 05:54:17 EDT Distribution: comp.lang.c Organization: University of Minnesota, Minneapolis Lines: 33 Keywords: variant records Summary: Genuine C Question I have an application where I want to represent nodes with a finite, fixed number of possible fields, e.g., applications equivalent to pascal's variant records. Some of these nodes will be relatively small, others large. I would like to allocate only enough memory for the union to hold the info necessary. Is there any portable, COMPACT way to do this in C? E.g., I have nodes which look like: struct node { struct this_stuff x; struct that_stuff y; struct othr_stuff z; union var_part { struct childrenA a[27]; struct childrenB b[3]; struct childrenC c[42]; } c; }; I would like to be able to make a call something like nodeptr = malloc(sizeof(node)-sizeof(node.c)+sizeof(node.c.b)) to reserve just enough space to hold fields x, y, z, and c.b[3]. It seems that the above expression might allocate more space than necessary (due to packing considerations), but should work. It is also a kluge. Is there a more elegant method? Besides using pascal? -- Robert Herndon Dept. of Computer Science, ...!ihnp4!umn-cs!herndon Univ. of Minnesota, herndon@umn-cs.ARPA 136 Lind Hall, 207 Church St. SE herndon.umn-cs@csnet-relay.ARPA Minneapolis, MN 55455