Object Model
An object is a region of data storage with a specific type of data that it holds. C programs create, access, modify, and destroy objects.
Objects have the following properties:
- Type
The type of an object determines its size and alignment characteristics, and also controls the relationship between its representation and its value. Objects with incomplete types–e.g.
void
–cannot exist. Most objects have an explicitly declared type which cannot change. In contrast, allocated objects obtain effective types when they are used, and their types may change.
- Size
The size of an object is the number of contiguously addressable bytes that make up that object. The
sizeof
operator be used to determine the size of an object or complete type.
- Representation
The raw data held in an object is called its object representation, which can be thought of as an array of raw bytes–
unsigned char[n]
, wheren
is the size of that object. The object representation of an object may be accessed directly through an lvalue with character type.
- Alignment
An object’s alignment requirement is the minimum address difference required between itself and another object of the same type. Alignment is always a power of two, and can result in padding between objects in order to satisfy alignment requirements.
- Storage Duration
An object’s storage duration determines its lifetime, which is the specific portion of the program that object is guaranteed to exist for,
Storage Duration
Lifetime
Static
Duration of the program
Automatic
Duration of the block in which the object is declared
Allocated
Until explicitly deallocated
- Value
The value of an object is the meaningful information it holds, and is determined by interpreting its object representation with respect to its type. Two objects with the same type and representation have the same value.
The relationship between object representation and value is implementation defined, and data must always be marshalled when it is read or written externally.
- Address
An integer representing the location of an object in memory, accessed with the
&
(address-of) operator. Two objects with the same type and same address are the same object. An object’s address does not change.Two objects with different type can have the same address while being different objects. For example, an array and its first element have the same address, but are fundamentally different objects with different types.