1

Well, I've created a bit field

typedef struct 
{
    unsigned bit : 1;
}BIT;

and the sizeof operator returns 4...ergo said struct isn't really a bit, rather 32. Is there a way to force a c struct to be below said value? is it all system dependent? (my book for one says a char is only 1 byte, whereas for me sizeof(char) returns 4...)

x86ris
  • 61
  • 1
  • 5
  • 1
    I'm dubious that your 'sizeof(char)' returns 4. – Charlie Burns Oct 02 '13 at 15:42
  • Size of char is not necessarily `1` byte always. It depends on machine architecture. – haccks Oct 02 '13 at 15:43
  • 3
    Probably you had `sizeof('a')` or so as returning 4. Character constants in C are not of type `char` but of type `int`. – Jens Gustedt Oct 02 '13 at 15:44
  • 3
    @haccks, no, no, no. `sizeof` is *defined* to return the number of `char` in an object and so `sizeof(char)` is always 1. If your compiler is telling you something different, it is definitively not a C compiler. – Jens Gustedt Oct 02 '13 at 15:44
  • @JensGustedt; Interesting! You are right. Then how could I calculate size of `char`? – haccks Oct 02 '13 at 15:53
  • 3
    @haccks: type `char` has size 1, by definition; the macro `CHAR_BITS` tells you how many bits are in a single `char` (at least 8, but could be more). Whether a `char` value maps to a single byte on the target platform is up to the implementation. For example, one old architecture (PDP?) used 36-bit words, which could hold 5 7-bit character values with one bit left over. Mapping a `char` value on to that architecture would be a challenge. – John Bode Oct 02 '13 at 16:03
  • @JohnBode; I read that on some machine `char` is of 2 bytes. Then how could I calculate this size ? – haccks Oct 02 '13 at 16:19
  • 1
    @haccks In C a "byte" is not necessarily 8 bits but "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment". It also must be >= 8 bits. IWO, a `char`. So `char` always has a size of 1. Outside of this C language definition, a "byte" is now largely understood to be 8 bits (it was not always so). So a `char` could be 2 "outside of C definition" bytes. – chux - Reinstate Monica Oct 02 '13 at 16:34
  • @haccks: A C `char` does not necessarily have to map to an 8-bit byte on the target machine. Imagine a word-addressed architecture where the smallest addressable unit of storage is 16 bits wide (I think some Crays are an example of this). A single `char` value will be mapped to a 16-bit word. Or you may be thinking of the `wchar` type, which was introduced to handle character sets that couldn't fit in ASCII or EBCDIC (and which has been superceded by Unicode, UTF-8, UTF-16, etc.). – John Bode Oct 02 '13 at 17:16

5 Answers5

3

N1570:

6.7.2.1 Structure and union specifiers
...
11 An implementation may allocate any addressable storage unit large enough to hold a bitfield. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

Your implementation is apparently allocating a 32-bit unsigned int to contain the bit field. Check your compiler documentation to see if there's a way to force it to use a smaller type for bit-fields, although I wouldn't hold my breath.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • so...essentially my computer is allocating the memory size of an unsigned int to just unsigned? if so...that makes sense, thanks. And im using gcc – x86ris Oct 02 '13 at 21:46
  • @user2646981, `unsigned int` is the same as `unsigned` which comes from C. – CS Pei Oct 02 '13 at 21:56
1

You may try to use unsigned char or short for the struct but there is also alignment issue that.

CS Pei
  • 10,869
  • 1
  • 27
  • 46
1

It is guaranteed by the standard for sizeof(char) to return 1.

To your BIT structure: It is not possible to define a data type that would require less than 1 byte of memory.

And to the bitfields (: specifier), if you really really really have to save some memory in all possible ways, you might be lucky enough to do something like this:

typedef struct 
{
    char c1 : 4;
    char c2 : 4;
} MyPackedByte;

which would allow you to store two 4-bit values within a single byte, yet behavior of this might be compiler specific and maybe you will have to use some preprocessor directives such as #pragma pack

see: #pragma pack effect

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • This is really good! Thanks. When I tried pragma pack(1) and told the main function to return sizeof(BIT)...it returned 1! – x86ris Oct 02 '13 at 21:57
0

Why do you have a struct if you only need 1 bit? The smallest unit of storage is a byte (typically 1 char); why not typedef BIT to be a char?

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • its just an experiment of curiosity. What if i had a really tiny amount of storage to work it, and all i needed was a boolean value...why waste 8 bits when i only need 1? – x86ris Oct 02 '13 at 21:51
0

you shouldn't be able to use sizeof() function for a bitfield. You are the one setting how you "cut" your int, maybe you could store it somewhere if you really need to get size of it later? Something like typedef struct { ``unsigned bit : 1, bit2 : 3;

``size_t size_bit1, size_bit2; }BIT;

void initBIT() {
size_bit1 = 1; size_bit2 = 3; }

toor
  • 33
  • 1
  • 6