tag 标签: standard c

相关博文
  • 热度 11
    2011-3-11 15:38
    1682 次阅读|
    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 one of my blogs, I explained how some of these elements can alter the physical layout of class objects. 1 This month, I'll expand my discussion of alignment, padding and member ordering to include access control. Access specifiers C++ class members can have different levels of accessibility: - A public member of a class is accessible anywhere in the program where the class itself is visible and accessible. - A private member of a class is accessible only to members and friends of that class. - A protected member of a class is accessible only to members and friends of that class or to members and friends of classes derived from that class. For example, a class widget defined as: class widget { public: widget(); protected: ~widget(); private: char m1; int m2; char m3; static int k; }; has one public member function (a default constructor), one protected member function (a destructor), and four private data members (three non-static and one static). Each access-specifier can appear more than once, and in any order. For example, you could rewrite the previous definition for widget as: class widget { protected: ~widget(); private: char m1; int m2; private: char m3; static int k; public: widget(); }; Here the keyword private appears twice and the public member is at the end (rather than the beginning) of the class definition. These changes aren't likely to alter the size or alignment of widg et objects. Member allocation order Within a given C structure, the members have offsets that increase in the order in which they are declared. In C++, a structure is just a class in which the members (and base classes) are public by default. Thus, in a class with only public members, such as: class widget { public: widget(); ~widget(); char m1; int m2; char m3; static int k; }; compilers allocate storage within each widget object for the non-static data members m1 , m2 , and m3 in that order. As I explained bfore, the constructor, destructor and static data member k add no storage to each widget object. What about a class with private or protected members? The relevant words in the draft of the next C++ Standard differ somewhat from the words in the current (2003) C++ Standard. I believe the draft's words are a better description of current practice. The draft states, "Non-static data members of a ... class with the same access control are allocated so that later members have higher addresses within a class object." 2 Storage allocation for a class in which all non-static data members have the same access—all public, or all private, or all protected—behaves just like storage allocation in a C structure. For example, the storage layout for widget objects when widget is defined as: class widget { public: widget(); ~widget(); static int k; private: char m1; int m2; char m3; }; is the same as when widget is defined as: class widget { public: widget(); ~widget(); char m1; int m2; char m3; static int k; }; which is the same as when widget is defined as: struct widget { char m1; int m2; char m3; }; These different class definitions aren't functionally equivalent, but they yield objects with the same size, alignment and member arrangement. Freedom to rearrange things The draft Standard also states that "The order of allocation of non-static data members with different access control is unspecified." This means that, if it wishes, a compiler can allocate members with different accessibility to reduce or eliminate the need for padding. For example, suppose class widget is defined as: class widget { public: char m1; protected: int m2; private: char m3; }; 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 can allocate storage for the members in the order in which they're declared and insert padding as if the class were defined as: class widget { public: char m1; char padding_after_m1 ; protected: int m2; private: char m3; char padding_after_m3 ; }; This layout requires six bytes of padding. On the other hand, the compiler can reorder the members to reduce the need for padding bytes, as either: class widget { public: char m1; private: char m3; char padding ; protected: int m2; }; or as: class widget { protected: int m2; public: char m1; private: char m3; char padding ; }; Using either rearrangement eliminates four padding bytes and shrinks the size of each widget object by that much. A C++ compiler can reorder members with different access specifiers, but can't reorder members within the same access specifier. For example, given: class widget { public: char m1; int m2; private: char m3; }; the compiler can't reorder the members so that m2 is allocated before m1 , as in: class widget { public: int m2; char m1; private: char m3; }; However, it can reorder the members so that m3 is before both m1 and m2 , as in: class widget { private: char m3; public: char m1; char padding ; int m2; }; None of the compilers that I have appear to rearrange members this aggressively. At least, I haven't been able to discover compiler options that will trigger such optimizations. If you have a compiler that will do this, please let me know which compiler(s) and compile option(s) you used to obtain this optimization. Endnotes: 1. Saks, Dan. " Classes are structure, and then some ," Embeddeddesignindia.com, March 2011. 2. Becker, Pete. Working Draft, Standard for Programming Language C++. ISO/IEC, February 2009.
  • 热度 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++.  
  • 热度 12
    2011-3-11 14:23
    2637 次阅读|
    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. In one of my blogs, I explained how some of these elements can alter the physical layout of class objects. 1 I'll soon expand my discussion of alignment, padding and member ordering to include access control. Access specifiers C++ class members can have different levels of accessibility:   - A public member of a class is accessible anywhere in the program where the class itself is visible and accessible. - A private member of a class is accessible only to members and friends of that class. - A protected member of a class is accessible only to members and friends of that class or to members and friends of classes derived from that class. For example, a class widget defined as: class widget { public: widget(); protected: ~widget(); private: char m1; int m2; char m3; static int k; }; has one public member function (a default constructor), one protected member function (a destructor), and four private data members (three non-static and one static). Each access-specifier can appear more than once, and in any order. For example, you could rewrite the previous definition for widget as: class widget { protected: ~widget(); private: char m1; int m2; private: char m3; static int k; public: widget(); }; Here the keyword private appears twice and the public member is at the end (rather than the beginning) of the class definition. These changes aren't likely to alter the size or alignment of widg et objects. Member allocation order Within a given C structure, the members have offsets that increase in the order in which they are declared. In C++, a structure is just a class in which the members (and base classes) are public by default. Thus, in a class with only public members, such as: class widget { public: widget(); ~widget(); char m1; int m2; char m3; static int k; }; compilers allocate storage within each widget object for the non-static data members m1 , m2 , and m3 in that order. As I explained bfore, the constructor, destructor and static data member k add no storage to each widget object. What about a class with private or protected members? The relevant words in the draft of the next C++ Standard differ somewhat from the words in the current (2003) C++ Standard. I believe the draft's words are a better description of current practice. The draft states, "Non-static data members of a ... class with the same access control are allocated so that later members have higher addresses within a class object." 2 Storage allocation for a class in which all non-static data members have the same access—all public, or all private, or all protected—behaves just like storage allocation in a C structure. For example, the storage layout for widget objects when widget is defined as: class widget { public: widget(); ~widget(); static int k; private: char m1; int m2; char m3; }; is the same as when widget is defined as: class widget { public: widget(); ~widget(); char m1; int m2; char m3; static int k; }; which is the same as when widget is defined as: struct widget { char m1; int m2; char m3; }; These different class definitions aren't functionally equivalent, but they yield objects with the same size, alignment and member arrangement. Freedom to rearrange things The draft Standard also states that "The order of allocation of non-static data members with different access control is unspecified." This means that, if it wishes, a compiler can allocate members with different accessibility to reduce or eliminate the need for padding. For example, suppose class widget is defined as: class widget { public: char m1; protected: int m2; private: char m3; }; 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 can allocate storage for the members in the order in which they're declared and insert padding as if the class were defined as: class widget { public: char m1; char padding_after_m1 ; protected: int m2; private: char m3; char padding_after_m3 ; }; This layout requires six bytes of padding. On the other hand, the compiler can reorder the members to reduce the need for padding bytes, as either: class widget { public: char m1; private: char m3; char padding ; protected: int m2; }; or as: class widget { protected: int m2; public: char m1; private: char m3; char padding ; }; Using either rearrangement eliminates four padding bytes and shrinks the size of each widget object by that much. A C++ compiler can reorder members with different access specifiers, but can't reorder members within the same access specifier. For example, given: class widget { public: char m1; int m2; private: char m3; }; the compiler can't reorder the members so that m2 is allocated before m1 , as in: class widget { public: int m2; char m1; private: char m3; }; However, it can reorder the members so that m3 is before both m1 and m2 , as in: class widget { private: char m3; public: char m1; char padding ; int m2; }; None of the compilers that I have appear to rearrange members this aggressively. At least, I haven't been able to discover compiler options that will trigger such optimizations. If you have a compiler that will do this, please let me know which compiler(s) and compile option(s) you used to obtain this optimization. Endnotes: 1. Saks, Dan. " Classes are structure, and then some ," Eetasia.com, March 2011. 2. Becker, Pete. Working Draft, Standard for Programming Language C++. ISO/IEC, February 2009.
  • 热度 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++.  
  • 热度 11
    2011-3-10 10:56
    1797 次阅读|
    0 个评论
    Here's how delete-expressions interact with destructors and deallocation functions in C++. I wrote a couple of articles on the distinction between allocating objects and allocating raw storage. In essence, when a program allocates an object, it not only allocates storage for the object but also initializes that storage with a value appropriate for the type of object that will occupy that storage. When a program just allocates storage, it leaves that storage uninitialized. Calling storage "raw" is a common way to emphasize that it's not initialized. The distinction between storage and objects is readily apparent in the behavioral difference between the Standard C malloc function and a C++ new-expression. Specifically, a C (or C++) expression such as: pw = (widget *)malloc(sizeof(widget)); allocates raw storage. Indeed, the malloc call allocates storage that's big enough and suitably aligned to hold an object of type widget , but it leaves that storage uninitialized. In contrast, a C++ new-expression as in: pw = new widget (); creates a widget object with a coherent initial value. The distinction between storage and objects carries over when allocating array objects. A call to malloc as in: pw = (widget *)malloc(10 * sizeof(widget)); allocates raw storage for an array of 10 widget s. By contrast, the C++ array new-expression in: pw = new widget ; allocates an array of 10 properly initialized widget s. Each new-expression is conceptually, if not actually, a two-step process: (1) allocate storage for an object, and (2) initialize it. For objects of class types, initializing an object usually involves calling a constructor. An array new-expression is also a two-step process, but the second step is a loop that initializes every array element. It should come as no surprise that the distinction between objects and raw storage also comes into play when you deallocate them. Just as allocating an object in C++ may involve calling a constructor, deallocating an object may involve calling a destructor. This month I'll explain how delete-expressions interact with destructors and deallocation functions in C++. I'll also explain how C programmers can employ a style of memory deallocation that mimics the behavior of C++ delete-expressions. Delete-expressions and destructors In C++, a destructor is a special class member function that "destroys" objects of that class type. Generally, to destroy an object means to deallocate or release the resources used by that object. A destructor's function name is always the same as its class name, but starting with a ~ (tilde, or "squiggle" if you prefer) as in: class widget { public: widget(); // a constructor ~widget(); // a destructor // ... }; Just as constructors provide guaranteed initialization for class objects, destructors provide guaranteed destruction. Just as you don't write explicit calls to constructors—compilers generate them for you—you don't write explicit destructor calls either. (Actually, you can write explicit destructor calls, but it's something you should avoid doing if possible.) Whenever you define an object with a class type, the compiler automatically plants calls to the object's destructor at the point(s) in the program where that object's lifetime ends—typically at function return statements and at the end (the closing brace) of function bodies and statement blocks. For guaranteed destruction to really be guaranteed, the compiler must generate a call to a destructor at every point where the program destroys an object, including in delete-expressions. Thus, if pw is a pointer to a single widget , a delete-expression such as: delete pw; doesn't just deallocate storage for that widget ; it applies widget 's destructor to the object addressed by pw to release any resources that the widget may have in its possession. Default initialization for scalar types, such as int and double , does nothing. When you dynamically allocate an object of scalar type, as in: int *pi = new int; the allocated object remains uninitialized. Default destruction for scalar types does nothing as well. An object of scalar type consumes no resources beyond its own storage, so there's nothing for its "destructor" to do. Thus, a delete-expression such as: delete pi; just deallocates the storage for the int addressed by pi . As I explained in previous blog Destroying everything in path , a destructor doesn't deallocate the storage for an object. 1 It releases the resources managed by the object, some of which may be storage. The storage for the object being destroyed will be deallocated by some other runtime agent of the program. For example, for a mythical widget object declared local to a function, the destructor for that widget executes just before the function returns. The destructor releases the widget 's resources, and then the runtime system deallocates the widget 's storage by popping it off the stack along with the rest of the function's stack frame. Just as the array new-expression in: pw = new widget ; applies the default widget constructor to each element in the storage allocated for the array, an array delete-expression such as: delete pw; applies the widget destructor to each array element. Whereas an array new-expression applies the constructors to the array element in ascending order by subscript starting at the 0th element, an array delete-expression applies the destructors in the reverse order. Delete-expressions and operator delete Just as a new-expression allocates memory by calling a function named operator new (rather than malloc ), a delete-expression deallocates memory by calling a function named operator delete (rather than free ). Each C++ environment provides a default implementation for a global operator delete , declared in the standard header as: void operator delete(void *p) throw (); The empty exception specification : throw () at the end of the function heading indicates that operator delete isn't supposed to allow any exceptions to propagate from the function. That is, operator delete may throw exceptions of various types, but it will catch those other exceptions before they can escape to the calling environment. However, as Scott Meyers explains, an empty exception specification doesn't really guarantee that no exceptions will propagate. 2 If pw is a pointer to an object of class type widget , a delete-expression such as: delete pw; translates more-or-less into something like: if (pw != NULL) { pw-~widget(); operator delete(pw); }