YouTip LogoYouTip

C Macro Offsetof

# C Library Macro - offsetof() The `offsetof()` macro is a standardized feature of the C library defined in the `` header. It is used to determine the byte offset of a specific member from the beginning of its parent structure. --- ## Description The `offsetof()` macro expands to an integral constant expression of type `size_t`. This value represents the offset in bytes from the start of the structure type to the specified member. By using `offsetof()`, developers can write highly portable code that interacts directly with memory layouts, avoiding hardcoded offsets that can change across different architectures, compilers, or compilation flags due to structure padding and alignment. --- ## Syntax ```c offsetof(type, member-designator) ``` ### Parameters * **`type`**: This is the name of the structure (or union) type. * **`member-designator`**: This is the name of the member within the specified structure whose offset is to be calculated. ### Return Value The macro returns a value of type `size_t` which represents the offset of the specified `member-designator` within the `type` in bytes. --- ## Code Example The following example demonstrates how to use the `offsetof()` macro to inspect the memory layout of a structure. ```c #include #include struct address { char name; char street; int phone; }; int main() { // Print the byte offset of each member in the 'address' structure printf("Offset of 'name' in struct address = %zu bytes.\n", offsetof(struct address, name)); printf("Offset of 'street' in struct address = %zu bytes.\n", offsetof(struct address, street)); printf("Offset of 'phone' in struct address = %zu bytes.\n", offsetof(struct address, phone)); return 0; } ``` ### Output When compiled and executed, the program produces the following output: ```text Offset of 'name' in struct address = 0 bytes. Offset of 'street' in struct address = 50 bytes. Offset of 'phone' in struct address = 100 bytes. ``` *(Note: The exact offset of `phone` may vary on some platforms depending on compiler-specific alignment and padding rules, though in this specific case, 100 is typical for 32-bit alignment).* --- ## Key Considerations * **Memory Alignment and Padding:** The compiler often inserts padding bytes between structure members to ensure they align with the CPU's word boundaries. Because of this, the offset of a member is not always equal to the sum of the sizes of the preceding members. `offsetof()` dynamically accounts for this padding. * **Portability:** Using `offsetof()` prevents manual offset calculations, which are highly error-prone and non-portable across different hardware architectures (e.g., 32-bit vs. 64-bit systems). * **C++ Compatibility:** In C++, `offsetof()` is also available via ``. However, according to the C++ standard, applying `offsetof()` to non-standard-layout classes (e.g., classes with virtual functions or private/protected base classes) results in undefined behavior. --- ## Common Application Scenes The `offsetof()` macro is widely used in systems programming and low-level software development: 1. **Custom Memory Allocators & Serialization:** When serializing structures to disk or transmitting them over a network, `offsetof()` helps map raw byte streams back into structured data. 2. **Hardware Register Mapping:** When writing device drivers, `offsetof()` is used to align software structures precisely with memory-mapped hardware registers. 3. **Intrusive Data Structures:** It is essential for implementing intrusive data structures (such as the doubly-linked lists in the Linux Kernel). By using `offsetof()`, you can retrieve a pointer to a parent structure from a pointer to one of its members (often wrapped in a `container_of` macro). 4. **Database Engines and Serialization Frameworks:** Used to dynamically inspect and manipulate fields within generic data records.
← Redis TutorialC Macro Null β†’