[英]unused memory using 32 bit integer in C
I have the folowing struct of integers (32 bit environment): 我有以下整数结构(32位环境):
struct rgb {
int r;
int g;
int b;
};
Am I correct in saying that, since rgb component values (0-255) only require 8-bits(1 byte) to be represented, I am only using 1 byte of memory and leaving 3 bytes unused for each component? 我是否正确地说,由于rgb组件值(0-255)仅需要表示8位(1字节),因此我仅使用1字节的内存,而对每个组件未使用3个字节?
Also, if I instead did the following: 另外,如果我改为执行以下操作:
struct rgb{
unsigned int r:8;
unsigned int g:8;
unsigned int b:8;
};
Assuming that what I said above is correct, would using this new struct reduce the number of unused bytes to 1? 假设我上面所说的是正确的,是否可以使用此新结构将未使用的字节数减少到1?
I would use unsigned char
, which is exactly what you need. 我将使用
unsigned char
,这正是您所需要的。 Like 喜欢
#ifndef BYTE
#define BYTE unsigned char
#endif
struct rgb
{
BYTE r;
BYTE g;
BYTE b;
};
But to answer your question - yes, it does reduce the number of bytes to 1 for each field. 但是要回答您的问题-是的,它的确会将每个字段的字节数减少为1。
Anyway, the struct will probably be with size of 4B, because of the alignment (but these are details and it's completely platform specific)
removed, thanks to @JimBuck's comment
无论如何,
由于@JimBuck的评论
,由于对齐(但是这些是细节,并且完全是特定于平台的),该结构可能具有4B的大小。
Yes that is correct. 对,那是正确的。 The second solution will essentially break down the struct to a single int with 8bits for each member.
第二种解决方案将本质上将结构分解为单个整数,每个成员具有8位。
#include "stdio.h"
struct rgb{
unsigned int r:8;
unsigned int g:8;
unsigned int b:8;
};
int main(void) {
printf("Size of RGB: %d", sizeof(struct rgb));
return 0;
}
This will output: "Size of RGB: 4". 这将输出:“ RGB大小:4”。 But as others have suggested it will be best to use a datatype that comes with 8bits, ergo:
unsigned char
. 但正如其他人所建议的那样,最好使用8bits附带的数据类型ergo:
unsigned char
。
Integers are 32 bit -> 4 bytes. 整数是32位-> 4个字节。 I would use unsigned char instead, which is only 1 byte containing values 0-255.
我会改用unsigned char,它只有1个字节,包含值0-255。
Using typedef to define a new name for the type makes it more convenient to use. 使用typedef为类型定义新名称可以使其更方便使用。
typedef unsigned char t_byte;
struct rgb {
t_byte r;
t_byte g;
t_byte b;
};
Unsigned int is still the same size, you want unsigned char Unsigned int仍然是相同大小,您想要unsigned char
struct rgb{
unsigned char r;
unsigned char g;
unsigned char b;
};
However, you should remember that the standard does not enforce they are not larger -- they may be bigger anyway. 但是,您应该记住,该标准并没有强制要求它们不更大-它们无论如何都可能更大。 You might need to use a packed pragma to get exactly the alignment you want.
您可能需要使用打包的编译指示来获得所需的精确对齐。
You could also use unsigned char rgb[3]
您也可以使用
unsigned char rgb[3]
In the first sample you are allocating 4 bytes per color component. 在第一个样本中,每个颜色分量分配4个字节。 For 8 bit color you should allocate just a single byte by using unsigned char.
对于8位颜色,应使用无符号char分配仅一个字节。 No need for bitfields here.
这里不需要位域。
Note that you have defined a 24 bit pixel here. 请注意,您在此处定义了24位像素。 You may need to add padding if you are using 32 bit pixels.
如果使用32位像素,则可能需要添加填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.