[Asterisk-Dev] Patching flags to bitfield structs

Andrew Thompson asteriskuser at aktzero.com
Thu Jan 20 15:32:06 MST 2005


Tilghman Lesher wrote:
> Easily proven:

Thanks.

This is why I'm not writing code, just looking for places where someone 
can tell me "if you see X, replace it with Y."

<snip code, replaced with a change>

/******************************************************************/
#include <stdio.h>

int main(void)
{
	struct mybits {
		unsigned int foo:1;
		unsigned int bar:2;
	} mybits = { 0, 0 };

	printf("foo=%d bar=%d\n\n", mybits.foo, mybits.bar);
	/* added above to show where variables start */

	mybits.foo = 5; /* Tilghman's bittest just incremented */
	mybits.bar = 5; /* I wanted to see what happened if you direct assigned */
	printf("foo=%d bar=%d\n", mybits.foo, mybits.bar);
	}
}

$ ./bittest3
foo=0 bar=0

foo=1 bar=1

I wanted to see if it would error if you knowingly assigned a value that 
was too large.

Care will need to be taken to make sure that a variable that asterisk 
toys with itself(like a counter) doesn't mistaken for a flag and 
accidently truncated to a small number of bits.

-- 
Andrew Thompson
http://aktzero.com/
http://dev.asteriskdocs.org/



More information about the asterisk-dev mailing list