Pointers on C——10 Structures and Unions.15

10.6.1 Variant Records

变体记录

Letʹs examine an example that implements what Pascal and Modula call a variant record. Conceptually, this is the same situation we just discussed—a particular area in memory will store different types of values at different times. In this case, however,the values are more complex than simple integers or floats. Each is an entire structure.

让我们讨论一个例子,实现一种在Pascal 和Modula 中被称为变体记录(variant record)的东西。从概念上说,这就是我们刚刚讨论过的那个情况——内存中某个特定的区域将在不同的时刻存储不同类型的值。但是,在现在这个情况下,这些值比简单的整型或浮点型更为复杂。它们的每一个都是一个完整的结构。


I took the following example from an inventory system that keeps track of two different kinds of entities: parts and subassemblies. A part is a gadget that is purchased from another manufacturer. It has various characteristics such as who we buy it from, how much it costs, and so forth. A subassembly is something that we make, and is a combination of a bunch of parts and other subassemblies.

下面这个例子取自一个存货系统,它记录了两种不同的实体:零件(part)和装配件(subassembly) 。零件就是一种小配件,从其他生产厂家购得。它具有各种不同的属性如购买来源、购买价格等。装配件是我们制造的东西,它由一些零件及其他装配件组成。


The first two structures specify what must be stored for a part and for a subassembly.

前两个结构指定每个零件和装配件必须存储的内容。


struct PARTINFO {

int cost;

int supplier;

...

};


struct SUBASSYINFO {

int n_parts;

struct {

char partno[10];

short quan;

} parts[MAXPARTS];

};


The inventory record contains common information for each entry and a union to store either part information or subassembly information.

接下来的存货( inventory )记录包含了每个项目的一般信息,并包括了一个联合,或者用于存储零件信息,或者用于存储装配件信息。


struct INVREC {

char partno[10];

int quan;

enum { PART, SUBASSY } type;

union {

struct PARTINFO part;

struct SUBASSYINFO subassy;

} info;

};


Here are some statements that manipulate an INVREC structure variable called rec.

这里有一些语句,用于操作名叫rec 的INVREC 结构变量。


if( rec.type == PART ){

y = rec.info.part.cons;

z = rec.info.part.supplier;

}

else {

y = rec.info.subassy.nparts;

z = rec.info.subassy.parts[0].quan;

}

Although not very realistic, this code illustrates how to access each of the members of the union. The first part of the statement gets the cost and the supplier for a part, and the second part of the statement gets the number of different parts in a subassembly and the quantity of the first part.

尽管并非十分真实,但这段代码说明了如何访问联合的每个成员。语旬的第1 部分获得成本(cost)值和零件的供应商(supplier) ,语旬的第2 部分获得一个装配件中不同零件的编号以及第1 个零件的数量。


In a union whose members are different sizes, the amount of memory allocated for the union is determined by the size of its largest member. Thus, a union will always be big enough to store its largest member. If the members are wildly different sizes, there will be a lot of wasted memory when storing the shorter members. In this type of situation, it might be preferable for the union to store pointers to the different members rather than the members themselves. The pointers would all be the same size thus avoiding the problem of wasted memory. When it is decided which member is needed, the right amount of memory can be obtained to store it. Chapter 11, which deals with dynamic memory allocation, contains an example illustrating this technique.

在一个成员长度不同的联合里,分配给联合的内存数量取决于它的最长成员的长度。这样,联合的长度总是足以容纳它最大的成员。如果这些成员的长度相差悬殊,当存储长度较短的成员时,浪费的空间是相当可观的。在这种情况下,更好的方法是在联合中存储指向不同成员的指针而不是直接存储成员本身。所有指针的长度都是相同的,这样就解决了内存浪费的问题。当它决定需要使用哪个成员时,就分配正确数量的内存来存储它。第11 章将讲述动态内存分配,它包含了一个例子用于说明这种技巧。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值