tag 标签: structure

相关博文
  • 热度 15
    2011-3-11 15:37
    1809 次阅读|
    0 个评论
    For the most part, C code that defines and uses structures behaves the same when compiled and executed as C++. However, C++ generalizes structures into classes. A C++ class can have elements that a C structure cannot, such as access specifiers, member functions, static data members, and base classes. In my following blogs, I'll explain how some of these elements alter the physical layout of class objects. The C++ Standard says a lot about classes, but hardly anything about structures. C++ treats structures as just classes declared in a slightly different way. According to the C++ Standard, "A structure is a class defined with the struct ; its members and base classes ... are public by default." ii In a class defined with the keyword class , the members and bases are private by default. For example, in C++, the structure definition: struct widget { char m1; int m2; char m3; }; is actually equivalent to the class definition: class widget { public: char m1; int m2; char m3;} ; As a class, widget has the same size and alignment as it does as a structure. Each class member has the same size, allocation order, alignment and padding as it does in the structure. When compiled for a target machine in which each int occupies four bytes aligned to an address that's a multiple of four, the compiler will insert three bytes of padding after each of the char members, as if the class had been defined as: class widget { public: char m1; char padding_after_m1 ; int m2; char m3; char padding_after_m3 ; }; The keyword public is one of three possible access-specifiers, the others being private and protected . The access-specifiers themselves don't occupy any data storage. Non-virtual member functions don't occupy data storage either. (They do occupy code space.) Static data members do occupy data storage, but not in the objects of which they are members. Thus, adding either non-virtual member functions or static data members to a class doesn't alter the storage layout for objects of that class. For example, objects of a widget class defined as: class widget { public: widget(); // constructor ~widget(); // destructor char m1; int m2; char m3; static int k; // static data member }; have the same storage layout with or without these new members (the ones highlighted) . On the other hand, adding one or more virtual functions to a class that previously had none, as in: class widget { public: widget(); virtual ~widget(); // virtual destructor char m1; int m2; char m3; static int k;}; typically adds a hidden non-static data member of pointer type, called a vptr , thus increasing sizeof(widget) by the size of that pointer. The presence of the vptr has no impact on the size and alignment of any of the other data members, and typically no impact on the padding in the class. Adding a base class to a class that previously had none, as in: class widget: public gadget { public: widget(); virtual ~widget(); // virtual destructor char m1; int m2; char m3; static int k; }; effectively adds a hidden non-static data member of the base class type, called the base class sub-object , thus increasing sizeof(widget) by the size of a gadget plus any additional padding that may be needed. The C++ Standard says nothing about the placement of the base class sub-object and the vptr. Many compilers place the vptr at the beginning, followed by the base class sub-object and then the non-static data members. For example, the compiler might lay out the storage for objects of the derived widget class (just above) as if widget had been defined as: class widget { public: widget_vtable *vptr; gadget base_class_subobject; char m1; int m2; char m3; }; Others place the base class sub-object before the vptr. The compiler may insert padding as needed. If the gadget base class already has its own vptr, widget objects can use that vptr as their own, and the compiler can omit the allocation of a separate vptr in the widget itself. The classes in all of my examples thus far have only public members. C++ has additional rules governing the placement of members with different accessibility. Endnotes: 1. ISO/IEC Standard 14882:2003(E), Programming languages—C++.  
  • 热度 14
    2011-3-10 19:21
    2170 次阅读|
    0 个评论
    I once discussed Standard C's rules governing the alignment, padding and ordering of structure members. For the most part, C code that defines and uses structures behaves the same when compiled and executed as C++. However, C++ generalizes structures into classes. A C++ class can have elements that a C structure cannot, such as access specifiers, member functions, static data members, and base classes. This month, I'll explain how some of these elements alter the physical layout of class objects. The C++ Standard says a lot about classes, but hardly anything about structures. C++ treats structures as just classes declared in a slightly different way. According to the C++ Standard, "A structure is a class defined with the struct ; its members and base classes ... are public by default." ii In a class defined with the keyword class , the members and bases are private by default. For example, in C++, the structure definition: struct widget { char m1; int m2; char m3; }; is actually equivalent to the class definition: class widget { public: char m1; int m2; char m3;} ; As a class, widget has the same size and alignment as it does as a structure. Each class member has the same size, allocation order, alignment and padding as it does in the structure. When compiled for a target machine in which each int occupies four bytes aligned to an address that's a multiple of four, the compiler will insert three bytes of padding after each of the char members, as if the class had been defined as: class widget { public: char m1; char padding_after_m1 ; int m2; char m3; char padding_after_m3 ; }; The keyword public is one of three possible access-specifiers, the others being private and protected . The access-specifiers themselves don't occupy any data storage. Non-virtual member functions don't occupy data storage either. (They do occupy code space.) Static data members do occupy data storage, but not in the objects of which they are members. Thus, adding either non-virtual member functions or static data members to a class doesn't alter the storage layout for objects of that class. For example, objects of a widget class defined as: class widget { public: widget(); // constructor ~widget(); // destructor char m1; int m2; char m3; static int k; // static data member }; have the same storage layout with or without these new members (the ones highlighted) . On the other hand, adding one or more virtual functions to a class that previously had none, as in: class widget { public: widget(); virtual ~widget(); // virtual destructor char m1; int m2; char m3; static int k;}; typically adds a hidden non-static data member of pointer type, called a vptr , thus increasing sizeof(widget) by the size of that pointer. The presence of the vptr has no impact on the size and alignment of any of the other data members, and typically no impact on the padding in the class. Adding a base class to a class that previously had none, as in: class widget: public gadget { public: widget(); virtual ~widget(); // virtual destructor char m1; int m2; char m3; static int k; }; effectively adds a hidden non-static data member of the base class type, called the base class sub-object , thus increasing sizeof(widget) by the size of a gadget plus any additional padding that may be needed. The C++ Standard says nothing about the placement of the base class sub-object and the vptr. Many compilers place the vptr at the beginning, followed by the base class sub-object and then the non-static data members. For example, the compiler might lay out the storage for objects of the derived widget class (just above) as if widget had been defined as: class widget { public: widget_vtable *vptr; gadget base_class_subobject; char m1; int m2; char m3; }; Others place the base class sub-object before the vptr. The compiler may insert padding as needed. If the gadget base class already has its own vptr, widget objects can use that vptr as their own, and the compiler can omit the allocation of a separate vptr in the widget itself. The classes in all of my examples thus far have only public members. C++ has additional rules governing the placement of members with different accessibility. Endnotes: 1. ISO/IEC Standard 14882:2003(E), Programming languages—C++.  
相关资源
  • 所需E币: 0
    时间: 2022-5-14 15:01
    大小: 728.4KB
    DESIGNOFVARIABLESTRUCTURESTABILIZERUSINGPOLEASSIGNMENTTECHNIQUE
  • 所需E币: 0
    时间: 2022-5-9 15:10
    大小: 1.39MB
    AnalyticalStructureandCharacteristicsofSymmetricKarnik–MendelType-ReducedIntervalType-2FuzzyPIandPDControllers
  • 所需E币: 0
    时间: 2022-5-9 14:59
    大小: 264.79KB
    AnImprovedPIDControlStructureforUnstableProcesses
  • 所需E币: 1
    时间: 2022-5-9 14:50
    大小: 824.36KB
    Analternativestructurefornextgenerationregulatorycontrollers
  • 所需E币: 1
    时间: 2022-4-11 11:13
    大小: 5.79MB
    StructureandSynthesisofPIDControllers(AniruddhaDatta,Ming-TzuHo,ShankarP.Bhattacharyya)
  • 所需E币: 2
    时间: 2020-6-23 18:57
    大小: 2.87MB
    上传者: Goodluck2020
    应用手册_Bluetooth®LowEnergytreestructurenetwork.pdf
  • 所需E币: 0
    时间: 2020-5-31 09:46
    大小: 2.49MB
    上传者: 星空下的屋顶
    StructureAndInterpretationOfComputerPrograms.pdf
  • 所需E币: 4
    时间: 2019-12-25 15:01
    大小: 675.88KB
    上传者: quw431979_163.com
    Imagesareadescriptionofhowaparametervariesoverasurface.Forexample,standardvisualimagesresultfromlightintensityvariationsacrossatwo-dimensionalplane.However,lightisnottheonlyparameterusedinscientificimaging.Forexample,animagecanbeformedofthetemperatureofanintegratedcircuit,bloodvelocityinapatient'sartery,x-rayemissionfromadistantgalaxy,groundmotionduringanearthquake,etc.Theseexoticimagesareusuallyconvertedintoconventionalpictures(i.e.,lightimages),sothattheycanbeevaluatedbythehumaneye.Thisfirstchapteronimageprocessingdescribeshowdigitalimagesareformedandpresentedtohumanobservers.CHAPTERImageFormation&Display23Imagesareadescriptionofhowaparametervariesoverasurface.Forexample,standardvisualimagesresultfromlightintensityvariationsacrossatwo-dimensionalplane.However,lightisnottheonlyparameterusedinscientificimaging.Forexample,animagecanbeformedofthetemperatureofanintegratedcircuit,bloodvelocityinapatient'sartery,x-rayemissionfromadistantgalaxy,groundmotionduringanearthquake,etc.Theseexoticimagesareusuallyconvertedintoconventionalpictures(i.e.,lightimages),sothattheycanbeevaluatedbythehumaneye.Thisfirstchapteronimageprocessingdescribeshowdigitalimagesareformedandpresentedtohumanobservers.DigitalImageStructure……
  • 所需E币: 4
    时间: 2020-1-3 18:27
    大小: 889.64KB
    上传者: quw431979_163.com
    郑州鑫彩电子科技有限公司:http://www.xincai168.com;规格书\三星\LTI820HS-L01.pdf;5”OFG#WY##VshflilfdwlrqIhe1#34/#5339OFG#Ghyhorsphqw#Fhqwhu41#VshflilfdwlrqITEMLTI820PID(T7)RemarkL/CModeS-PVA(TT)TResolution1,920xRGBx1,080TActiveAreaX_W\U^]GGGXWX\U^[PixelSize/ppi0.3135x0.9405(27ppi)DisplayColor8bit/RGBTColorGamut72%(Typ.)TLuminance700nit(5.5mA)TContrastRatio1200:1ResponseTime8msec(DCC2)nTTnColorTem……
  • 所需E币: 4
    时间: 2019-12-24 23:29
    大小: 84.83KB
    上传者: 2iot
    Abstract:Thisapplicationnotediscussesthe1-Wire®FileStructure(OWFS).TheOWFSprovidesadirectorystructurefordataresidingin1-WiredevicesincludingiButtons.Itallowsnamedfilestoberandomlyaccessedastheyareonotherfilesystems.Thisdocumentprovidesthefullspecificationincludingbitmapfilesforlargecapacity1-Wiredevices,multiplesubdirectories,extendedfileattributes,andotherusefulconstructssuchasfilesspanningmultipledevices.1-WireFileStructureJul11,2001Abstract:Thisapplicationnotediscussesthe1-WireFileStructure(OWFS).TheOWFSprovidesadirectorystructurefordataresidingin1-WiredevicesincludingiButtons.Itallowsnamedfilestoberandomlyaccessedastheyareonotherfilesystems.Thisdocumentprovidesthefullspecificationincludingbitmapfilesforlargecapacity1-Wiredevices,multiplesubdirectories,extendedfileattributes,andotherusefulconstructssuchasfilesspanningmultipledevices.I.1-WireFileStructureThe1-WireFileStructure(OWFS)providesadirectorystructurefordataresidingin1-WiredevicesincludingiButtons.Itallowsnamedfilestoberandomlyaccessedastheyareonotherfilesystems.Thisdocumentprovidesthefullspecificationincludingbitma……
  • 所需E币: 4
    时间: 2020-1-13 18:08
    大小: 1.1MB
    上传者: 978461154_qq
    us_patent_striplinestructureandmethodwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.comwww.freepatentsonline.com……
  • 所需E币: 4
    时间: 2020-1-15 10:20
    大小: 409.7KB
    上传者: wsu_w_hotmail.com
    electronicstructureandmagne...,electronicstructureandmagnetoopticalpropertiesofsolids……
  • 所需E币: 4
    时间: 2020-1-13 16:34
    大小: 839.83KB
    上传者: quw431979_163.com
    HFSS(仿真软件)使用方法(HighFrequencyStructureSimulator)……