有什么好的例子可以说明结构体和联合体的区别吗? 基本上我知道struct使用了它成员的所有内存,而union使用了最大的成员内存空间。还有其他操作系统级别的差异吗?
当前回答
联合的用法 当需要特殊类型的对话时,联合经常被使用。 来了解联合的用处。c/c标准库没有定义专门为将短整数写入文件而设计的函数。使用fwrite()会导致简单操作的开销过大。然而,使用联合可以很容易地创建一个函数,该函数将一个短整数的二进制每次一个字节写入文件。我假设短整数是2字节长
示例:
#include<stdio.h>
union pw {
short int i;
char ch[2];
};
int putw(short int num, FILE *fp);
int main (void)
{
FILE *fp;
fp fopen("test.tmp", "wb ");
putw(1000, fp); /* write the value 1000 as an integer*/
fclose(fp);
return 0;
}
int putw(short int num, FILE *fp)
{
pw word;
word.i = num;
putc(word.c[0] , fp);
return putc(word.c[1] , fp);
}
虽然我用短整数调用putw(),但可以使用putc()和fwrite()。但是我想展示一个例子来说明如何使用联合
其他回答
简单的回答是:结构体是一个记录结构体:结构体中的每个元素分配新的空间。一个结构体
struct foobarbazquux_t {
int foo;
long bar;
double baz;
long double quux;
}
为每个实例在内存中分配至少(sizeof(int)+sizeof(long)+sizeof(double)+sizeof(long double))字节。(“至少”是因为架构对齐约束可能迫使编译器填充结构。)
另一方面,
union foobarbazquux_u {
int foo;
long bar;
double baz;
long double quux;
}
分配一个内存块并给它四个别名。因此sizeof(union foobarbazquux_u)≥max((sizeof(int),sizeof(long),sizeof(double),sizeof(long double)),同样有可能添加一些对齐。
As you already state in your question, the main difference between union and struct is that union members overlay the memory of each other so that the sizeof of a union is the one , while struct members are laid out one after each other (with optional padding in between). Also an union is large enough to contain all its members, and have an alignment that fits all its members. So let's say int can only be stored at 2 byte addresses and is 2 bytes wide, and long can only be stored at 4 byte addresses and is 4 bytes long. The following union
union test {
int a;
long b;
};
could have a sizeof of 4, and an alignment requirement of 4. Both an union and a struct can have padding at the end, but not at their beginning. Writing to a struct changes only the value of the member written to. Writing to a member of an union will render the value of all other members invalid. You cannot access them if you haven't written to them before, otherwise the behavior is undefined. GCC provides as an extension that you can actually read from members of an union, even though you haven't written to them most recently. For an Operation System, it doesn't have to matter whether a user program writes to an union or to a structure. This actually is only an issue of the compiler.
union和struct的另一个重要属性是,它们允许指向它们的指针可以指向其任何成员的类型。因此,以下是有效的:
struct test {
int a;
double b;
} * some_test_pointer;
Some_test_pointer可以指向int*或double*。如果将一个test类型的地址转换为int*,它将指向它的第一个成员,实际上是a。工会也是如此。因此,因为联合将始终具有正确的对齐方式,您可以使用联合来使指向某些类型的指针有效:
union a {
int a;
double b;
};
这个联合实际上可以指向int型和double型:
union a * v = (union a*)some_int_pointer;
*some_int_pointer = 5;
v->a = 10;
return *some_int_pointer;
实际上是有效的,正如C99标准所述:
对象的存储值只能由具有以下类型之一的左值表达式访问: 与对象的有效类型兼容的类型 ... 在其成员中包含上述类型之一的聚合或联合类型
编译器不会优化出v->a = 10;因为它可能会影响*some_int_pointer的值(该函数将返回10而不是5)。
联合的用法 当需要特殊类型的对话时,联合经常被使用。 来了解联合的用处。c/c标准库没有定义专门为将短整数写入文件而设计的函数。使用fwrite()会导致简单操作的开销过大。然而,使用联合可以很容易地创建一个函数,该函数将一个短整数的二进制每次一个字节写入文件。我假设短整数是2字节长
示例:
#include<stdio.h>
union pw {
short int i;
char ch[2];
};
int putw(short int num, FILE *fp);
int main (void)
{
FILE *fp;
fp fopen("test.tmp", "wb ");
putw(1000, fp); /* write the value 1000 as an integer*/
fclose(fp);
return 0;
}
int putw(short int num, FILE *fp)
{
pw word;
word.i = num;
putc(word.c[0] , fp);
return putc(word.c[1] , fp);
}
虽然我用短整数调用putw(),但可以使用putc()和fwrite()。但是我想展示一个例子来说明如何使用联合
有什么好的例子可以说明结构体和联合体的区别吗?
下面是来自嵌入式系统应用的真实例子。它只使用联合,但它清楚地显示了联合的功能。
I2C通信协议的写函数在从传递给它的数组中检索数据时使用联合类型,用于数据存储。
union data
{
uint32_t packet;
uint8_t packetbyte[4];
} txdata;
通过指针传递给write函数的数组包含一个字节大小的元素。在for循环中,这些字节分四个步骤逐一提取,并存储到txdata的各个元素中。packetbyte成员。
循环结束后,txdata。数据包包含4个字节的数据,这些数据被连续存储到txdata联合中。作为通过通信总线发送数据的最后一步,txdata。数据包被写入32位缓冲区,在被写入后,32位缓冲区启动写序列。然后通过txdata重置内容。在next for循环开始执行之前,Packet = 0。
通过这种方式,I2C主机可以重新传输32位数据包,直到发送通过的输入数据并终止写入功能。
结构和联合的区别是什么?
简单的回答是:区别在于内存分配。 解释: 在结构中,将为结构内部的所有成员创建内存空间。 在联合内存空间将只为需要最大内存空间的成员创建。 考虑下面的代码:
struct s_tag
{
int a;
long int b;
} x;
union u_tag
{
int a;
long int b;
} y;
这里在struct和union内部有两个成员:int和long int。在32位操作系统中,int的内存空间为:4字节,long int的内存空间为:8。
因此,对于struct, 4+8=12个字节将被创建,而对于union,将创建8个字节
代码示例:
#include<stdio.h>
struct s_tag
{
int a;
long int b;
} x;
union u_tag
{
int a;
long int b;
} y;
int main()
{
printf("Memory allocation for structure = %d", sizeof(x));
printf("\nMemory allocation for union = %d", sizeof(y));
return 0;
}
裁判:http://www.codingpractise.com/home/c-programming/structure-and-union/
推荐文章
- C多行宏:do/while(0) vs作用域块
- time_t最终的类型定义是什么?
- 我需要显式处理负数或零时,总和平方数字?
- 函数名周围的括号是什么意思?
- 用C语言创建自己的头文件
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- main()中的Return语句vs exit()
- 如果不是内存地址,C指针到底是什么?
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 保护可执行文件不受逆向工程的影响?
- 从C语言的函数返回一个struct
- C99 'restrict'关键字的实际用法?
- 检查子字符串存在于C中的字符串中
- 从标准输入中捕获字符,而不需要等待按enter键
- 查找当前可执行文件的路径,不包含/proc/self/exe