2

Given a complex data structure where each sub-structure has a variable that has a domain of {true or false},

(e.g.)

struct dataBlock{
    struct {
        /* more members */
        char status;
    } node1;

    struct {
       /* more members */
       char status;
    } node2;
    /* More nodes */
};

It would be a waste to have 1 byte just for a value of 1 or 0. Is there a C language technique that status in each node will only occupy a bit in a byte? What I can think of is by using MACROS but macros cannot be contained in a local scope right? So having macro status will mean only one macro status in the program. Hence, calling node1.status and node2.status uses the same macro.

Xegara
  • 563
  • 2
  • 10
  • 23
  • 1
    Duplicate: http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c – JBentley Jan 01 '14 at 08:20
  • 4
    dont bother yourself thinking about bits/bytes. Because there is padding taking place when you use different types of variables in structure. Also note that other languages use bytes for boolean data. – Fredrick Gauss Jan 01 '14 at 08:20
  • don't bother to think about saving memory in small programs since it won't give you any improvement but in fact may increase the program size and speed because it needs more instructions to deal with the bits instead of only one instruction on the whole int. Only the program and stack when you start often needs megabytes of memory and saving 1 or 2 bytes doesn't mean anything. Only for very large arrays that are accessed frequently that may create a lot of cache misses then you should change data representation to save memory – phuclv Jan 01 '14 at 09:45

2 Answers2

6

You can use a bitfield - this syntax allows you to define how many bits each int in a strcut should occupy. Note, however, that C can only allocate full bytes, so the size of the struct would be rounded up to the nearest multiplication of 8 bits in any case.

E.g.:

struct {
    int whole_int; /* a whole int, let's assume it's 16 bits. */
    int half_int : 8; /* only half an int */
    int another_half_int : 8;
} some_struct /* Total size is 2 bytes*/

Having said that, I sincerely doubt you'll notice any performance gain from using this technique, and as Fredrick Gauss commented, it's probably not worth the hassle.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Can I have an int pointer to point to half_int? Such that it would display the half_int and not the entire int? – Xegara Jan 01 '14 at 09:05
  • you could use a union of int and a struct of its bits so that you can access the whole int or the bits in it – phuclv Jan 01 '14 at 09:47
1

C has a built in feature called bit fields that will get the job done.

Basically, bit fields automatically optimizes a structure to use only as much memory as needed for each given member. In your case, you would do something like this.

struct statusNode {
  /* ... */

  /* only use 1 bit for this member */
  unsigned int status : 1;

  /* for example, test only needs 4 bits (range of 0 to 15) */
  unsigned int test : 4;
};

struct dataBlock {
    struct statusNode node1;

    /* ... */

    struct statusNode node2;
};

You can assign each members a certain number of bits based on the highest value that you'll ever come across.

You can find more information about bit fields here.

Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
  • The declared automatic switching to packed representation if a bitfield is present isn't valid at least for gcc, which expects explicit declaration. E.g. three consecutive 20-bit fields on a 32-bit machine will be placed in 3 4-byte words unless __attribute__((packed)) is set for the structure. – Netch Jan 01 '14 at 08:56