4

In C, I saw some usage of offsetof to calculate the offset of a member in the structure to its beginning?

Is it still recommended to use in C++? Any other way to do this without this macro?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • 1
    offsetof [can be used in C++](http://stackoverflow.com/a/19943445/1708801) with the restrictions that `If type is not a standard-layout class (Clause 9), the results are undefined` and `The result of applying the offsetof macro to a field that is a static data member or a function member is undefined.`. – Shafik Yaghmour Mar 28 '14 at 15:13
  • You should ask yourself seriously why you need the functionality of the "offsetof" macro. It is available in C++, but it will give unexpected results for non-POD structs and classes. If you don't know what a POD is then you better not use it in C++ code. You probably better not use it in C code either unless you have a very good reason. – gnasher729 Mar 28 '14 at 15:14
  • 3
    Why is everyone discouraging its use? It is useful when calculating offsets for inline assembly or for calculating offsets of a POD structure that can change (TEB structure of WINAPI in Winternl.h) and for calculating offsets in x32 vs. x64 code without hardcoding the offsets.. – Brandon Mar 28 '14 at 15:16
  • There is no harm in using it if you need it. I would advise against creating your own implementation if this is exactly what you need. It has official support in the C++ standard. I would suggest that you think about your design a bit more to see if you really need it. – NickC Mar 28 '14 at 15:20
  • @CantChooseUsernames: the real question, however, is *why would you need to know* ? I cannot, at the moment, think of a single problem that *requires* its use. – Matthieu M. Mar 28 '14 at 15:32
  • @MatthieuM. that is the problem with the question and the answers is we don't understand the use case. – Shafik Yaghmour Mar 28 '14 at 15:33
  • @MatthieuM. You really can't come up with ONE use-case? http://pastebin.com/7YZk26XK <--- Reading the PEB and TEB structure. With almost every windows revision, the offsets change. In case you are wondering, reading these structures let you know the reference count to a .dll, which .dll's are loaded in a process, their offsets and various different information about them. You can also HIDE a .dll in the process or unlink it. There are many legit use-cases for offset-of.. The above code produces the following output: http://i.imgur.com/DXacFdG.png and http://i.imgur.com/itIDAT2.png – Brandon Mar 28 '14 at 17:09
  • And to support my comment above, http://msdn.microsoft.com/en-us/library/windows/desktop/aa813706(v=vs.85).aspx It states the structure may change in future releases.. I've seen it change a lot. Currently, the structure looks like: http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/NT%20Objects/Process/PEB.html There are MANY more where that came from. TEB being one.. Diving into the world of undocumented WINAPI and changing structures makes offsetof one of the most valuable assets. Check out the MSDN link and read the post at the very bottom.. – Brandon Mar 28 '14 at 17:14
  • @CantChooseUsernames: I feel the need to point out that a function `void readLdr(HANDLE ProcessHandle, PEB_LDR_DATA* out);` could handle this with a much cleaner API. – Matthieu M. Mar 28 '14 at 17:31
  • I wasn't able to find such a function anywhere online :S If you mean that I could write on then yes I completely agree that what I posted is absolutely dirty. – Brandon Mar 28 '14 at 18:34
  • http://stackoverflow.com/questions/22726351/can-i-avoid-using-offsetof-in-the-following I link this question – Adam Lee Mar 29 '14 at 03:37
  • Since a lot of friends said offsetof is not recommmeeded, can the above question gets solved? – Adam Lee Mar 29 '14 at 03:38
  • @AdamLee the main problem is that we don't understand the problem you are trying to solve. If you can explain why you think you need `offsetof` you may get a better answer. – Shafik Yaghmour Mar 31 '14 at 14:27
  • Similar topic - http://stackoverflow.com/questions/400116/what-is-the-purpose-and-return-type-of-the-builtin-offsetof-operator – Dynite Apr 10 '14 at 21:08

5 Answers5

1

C++ has pointers to members. These are similar to offsets, but (1) typesafe and (2) more generic - they also work with methods, on base classes, etc.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

Offsetof is a feature that is only in the C++ standard for C compatibility so it is not reccomended to be used in C word

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

I wouldn't recommend it for anything, unless you really need to. So long as your use is valid in C, it should be valid for C++. Without knowing what you are doing, your question is impossible to answer.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
0

No, it's not.

Proper C++ style encourages user to avoid all low-level details when possible. This is both for safety/correctness and readability. Only when low-level manipulation guarantees real and significant performance improve should one jump into it.

WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
0

One of the usages of offsetof() was in C-like code having structures with a fixed header, and variable size arrays, like explained in this blog post:

Why do some structures end with an array of size 1?

(Note that this blog post uses the FIELD_OFFSET macro, which is a Windows-ism, corresponding to offsetof().)

I think in modern C++ we should use higher-level techniques and code, like std::vector for variable sized arrays.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162