unused memory using 32 bit integer in C

Posted by endmade on Stack Overflow See other posts from Stack Overflow or by endmade
Published on 2011-11-20T17:43:29Z Indexed on 2011/11/20 17:50 UTC
Read the original article Hit count: 158

Filed under:
|
|
|

I have the folowing struct of integers (32 bit environment):

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?

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c