Archived

This topic is now archived and is closed to further replies.

Shannon Barber

C Syntax I've never seen before

Recommended Posts

Shannon Barber    1681
Ok, why I have I never seen this before? And why didn''t I learn it in school? And why isn''t it in any of my C nor C++ books!? xps considering how friggin'' useful it is!
  
   typedef struct {
       unsigned int version:2;   /* protocol version */
       unsigned int p:1;         /* padding flag */
       unsigned int x:1;         /* header extension flag */
       unsigned int cc:4;        /* CSRC count */
       unsigned int m:1;         /* marker bit */
       unsigned int pt:7;        /* payload type */
       u16 seq;              /* sequence number */
       u32 ts;               /* timestamp */
       u32 ssrc;             /* synchronization source */
       u32 csrc[1];          /* optional CSRC list */
   } rtp_hdr_t;
  
Magmai Kai Holmlor "Oh, like you''ve never written buggy code" - Lee "What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
Floppy    122
Those : (colons) things set the bit width in the structure instead of using the default generated compiler bit widths. I think that''s what you didn''t understand.

Share this post


Link to post
Share on other sites
Cyberdrek    100
quote:
Original post by Magmai Kai Holmlor
Ok, why I have I never seen this before?
And why didn''t I learn it in school?
And why isn''t it in any of my C nor C++ books!?
xps considering how friggin'' useful it is!


    
typedef struct {
unsigned int version:2; /* protocol version */
unsigned int p:1; /* padding flag */
unsigned int x:1; /* header extension flag */
unsigned int cc:4; /* CSRC count */
unsigned int m:1; /* marker bit */
unsigned int pt:7; /* payload type */
u16 seq; /* sequence number */
u32 ts; /* timestamp */
u32 ssrc; /* synchronization source */
u32 csrc[1]; /* optional CSRC list */

} rtp_hdr_t;


Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

You''re talking about the Width??? ( ex. int value:2 ) I''ve seen it before but I''ve never used it personally though... But it''s even mentionned in my old Borland Turbo C++ v2.0 books. So I guess it''s probably old knowledge...



"And that''s the bottom line cause I said so!"

** I WANT TO BE THE MODERATOR FOR THE LINUX FORUM **

Cyberdrek

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash

Share this post


Link to post
Share on other sites
gmcbay    130
You must have bad C books. Try the K&R book.

Also beware of memory alignment issues. Doing your own bit-specification for memory sizes can be helpful in network protocols and when storing things to disk, but if you''re not careful it can hurt your CPU efficiency at runtime as most good compilers will try to pad structures out for best memory alignment and a programmer could possibly screw this up if they aren''t careful.

Share this post


Link to post
Share on other sites
Shannon Barber    1681
I immediately recognized what they did, I had just never seen code that used them before.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
That''s why I always recommend Stroustrup''s book. I knew absolutely no C++, just java, and within a week (I read pretty quickly) I knew more than the average person on this forum, and far more than the people in my new school who have taken 2 semesters of C and 1 semester of C++. Just buy that book and read it straight through, that''s all you need to do. (assuming you already have some programming experience)

Share this post


Link to post
Share on other sites
Kylotan    10010
Yep: reading and understanding the Stroustrup book will put you in the top 25% of C++ programmers on this board. Pretty much everything the language can do is explained and justified, with some nifty examples along the way.

Regarding bitfields, it''s important to remember that what you save on structure space, you lose in execution time (added shifts and masking), so it''s not a win/win situation.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
ugenn    122
quote:
Original post by gmcbay
You must have bad C books. Try the K&R book.

Also beware of memory alignment issues. Doing your own bit-specification for memory sizes can be helpful in network protocols and when storing things to disk, but if you''re not careful it can hurt your CPU efficiency at runtime as most good compilers will try to pad structures out for best memory alignment and a programmer could possibly screw this up if they aren''t careful.



Also be aware of endianees issues with bit-fields if your code is going to be multi-platform. Especially so when you''re using bitfields in conjunction with unions.

Share this post


Link to post
Share on other sites