[Standard library]_[Primary]_[calculates the offset of struct members]

Source: Internet
Author: User


Scene:

1. It is not necessary to calculate the offset of a struct in a C structure, but it is necessary to put it into the design of the plug-in structure, such as a scenario that can only be accessed using offsets, rather than a scene referencing member variables.

2. In the design of a consistent interface, the common interface does not change, but the structure of the plug-in module can not be designed according to the unified structure, they only need to provide offset to the public interface call on the line, different plug-ins

Possible offsets are inconsistent because they can be implemented independently. The public interface can access different variables through offsets.

3. You can use the Offsetof in the Stddef.h file


/* DEFINE OFFSETOF Macro */#ifdef __cplusplus#ifdef _win64#define offsetof (s,m) (size_t) (ptrdiff_t)   & Reinterpret_cast<const volatile Char&> (((((((s) 0)->m)) #else # define OFFSETOF (s,m)   (size_t) & Reinterpret_cast<const volatile Char&> ((((((s) 0)->m) #endif #else#ifdef  _win64#define offsetof (s,m )   (size_t) ((ptrdiff_t) & (((s) 0)->m)) #else # define OFFSETOF (s,m)   (size_t) & ((((s) 0)->m) # endif



FFmpeg part of the code:

Notice the offset here.

static const avoption options[]={{"B", "Set bitrate (in bits/s)", OFFSET (bit_rate), Ff_opt_type_int, Av_codec_default_ Bitrate, Int_min, Int_max, v| e},{"AB", "set bitrate (in bits/s)", OFFSET (bit_rate), Ff_opt_type_int, 64*1000, Int_min, Int_max, a| e},{"BT", "Set video bitrate tolerance (in bits/s)", OFFSET (bit_rate_tolerance), Ff_opt_type_int, Av_codec_default_ BITRATE*20, 1, Int_max, v| e},{"Flags", NULL, OFFSET (Flags), Ff_opt_type_flags, DEFAULT, 0, Uint_max, v| a| e| D, "Flags"},{"MV4", "use four motion vectors by Macroblock (MPEG4)", 0, Ff_opt_type_const, CODEC_FLAG_4MV, Int_min, Int_max , v| E, "Flags"},{"OBMC", "use overlapped block motion compensation (h263+)", 0, Ff_opt_type_const, CODEC_FLAG_OBMC, Int_min, I Nt_max, v| E, "Flags"},{"Qpel", "use section PEL Motion compensation", 0, Ff_opt_type_const, Codec_flag_qpel, Int_min, Int_max, v| E, "Flags"},{"loop", "Use Loop filter", 0, Ff_opt_type_const, Codec_flag_loop_filter, Int_min, Int_max, v| E, "Flags"},{"Qscale", "Use fixed Qscale", 0, Ff_opT_type_const, Codec_flag_qscale, Int_min, Int_max, 0, "Flags"},{"GMC", "use GMC", 0, Ff_opt_type_const, CODEC_FLAG_GMC, I Nt_min, Int_max, v| E, "Flags"},{"Mv0", "always try a megabyte with mv=<0,0>", 0, Ff_opt_type_const, Codec_flag_mv0, Int_min, Int_max, v| E, "Flags"},{"part", "Use data partitioning", 0, Ff_opt_type_const, Codec_flag_part, Int_min, Int_max, v| E, "Flags"},{"input_preserved", NULL, 0, Ff_opt_type_const, codec_flag_input_preserved, Int_min, Int_max, 0, "flags"},

To test the code, compile with vs2010:

void Testoffset () {//1. The compiler automatically aligns a multiple of its type size value to the structure body data type. struct T{char c;char b;int e;int e1;double D;};  cout << offsetof (t,b) << endl;cout << offsetof (t,e) << endl;cout << offsetof (t,e1) << Endl;cout << offsetof (t,d) << Endl;} int _tmain (int argc, _tchar* argv[]) {testoffset (); return 0;}

Output:

14816



[Standard library]_[Primary]_[calculates the offset of struct members]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.