aboutsummaryrefslogtreecommitdiffstats
path: root/doc/info/chap/language-facilities.texinfo
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-10-13 02:31:12 +0200
committerMattias Andrée <maandree@operamail.com>2015-10-13 02:31:12 +0200
commit4bd3c8a85c3366dcd22ee77bcf6c9f673ae91f66 (patch)
tree2c0c01cfeab1a1d06a619e66265070238585aaea /doc/info/chap/language-facilities.texinfo
parentadd todo about undefined behaviour of offsetof (diff)
downloadslibc-4bd3c8a85c3366dcd22ee77bcf6c9f673ae91f66.tar.gz
slibc-4bd3c8a85c3366dcd22ee77bcf6c9f673ae91f66.tar.bz2
slibc-4bd3c8a85c3366dcd22ee77bcf6c9f673ae91f66.tar.xz
info: member offsets
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'doc/info/chap/language-facilities.texinfo')
-rw-r--r--doc/info/chap/language-facilities.texinfo37
1 files changed, 37 insertions, 0 deletions
diff --git a/doc/info/chap/language-facilities.texinfo b/doc/info/chap/language-facilities.texinfo
index 81908e8..e30352e 100644
--- a/doc/info/chap/language-facilities.texinfo
+++ b/doc/info/chap/language-facilities.texinfo
@@ -8,6 +8,7 @@
* Booleans:: Proper booleans in the C programming language.
* Non-returning functions:: Specifying that functions never return.
* Variable alignment:: Aligment of variables and data types.
+* Member offsets:: Getting the offset of members of a structure.
@end menu
@@ -189,3 +190,39 @@ should have the alignment of the type @code{TYPE}.
The function call @code{alignof(TYPE)}, returns
the alignment of the type @code{TYPE}.
+
+
+@node Member offsets
+@section Member offsets
+
+@hfindex stddef.h
+@cpindex Member offsets
+@cpindex Structure member offsets
+@cpindex Offsets of structure member
+@fnindex @code{offsetof}
+@sc{ANSI}@tie{}C defines the macro @code{offsetof}
+to be declared by the header file @file{<stddef.h>}.
+It returns the offset, in bytes, of a direct or
+indirect member in a @code{struct} or @code{union}.
+The call @code{offsetof(type, member)} returns the
+offset of the member @code{member} in the type
+@code{type}.
+
+As en example, consider the structure
+@example
+struct example
+@{
+ char offset_is_0[16];
+ char offset_is_16[16];
+@}
+@end example
+@code{offsetof(struct example, offset_is_0)}
+evaluates to 0 because it is at the top of the
+structure. @code{offsetof(struct example, offset_is_16)}
+evaluates to 16 because the member above it in the
+structure has offset 0 and size 16. 0 + 16 = 16.
+
+@code{offsetof} does not support bit field-@code{struct}:s.
+@code{offsetof} is also known to be problematic in C++,
+because C++ supports redefining operators.
+