6935

Why use a numeric value in reinterpet_cast?

Question:

I've been looking at some AOSP code, and I came across this (from a 5.0+ branch, under art/runtime/base/macros.h)

#define OFFSETOF_MEMBER(t, f) \ (reinterpret_cast<const char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<const char*>(16)) // NOLINT

This is used in calls such as:

MemberOffset(OFFSETOF_MEMBER(Class, dex_cache ));

Where Class is a class and dex_cache is a member under that class. This returns the offset of the field and then is used as the constructor of a MemberOffset class.

My question is why on earth would you want to hardcode that "16" in there? Wouldn't it make more sense just take the address of the member and subtract the base address of the class?

Answer1:

You can't subtract base address of class. You could do that for base address of object, but you do not have any. So, the macro converts address 16 into that base address.

Why not 0? Lint would complain. Why not 1? Structure alignment problems.

Recommend

  • Using the type conversion operator
  • C++ Text File Reading
  • Cast boost::array for function with argument (const float (&arr)[12])
  • How to unpack 32bit integer packed in a QByteArray?
  • netsh acl setting (need alternative method - registry settings?)
  • how do i write assembly code from c#?
  • How to get latest version of a artifact on Bintray using JSONP
  • Invalid Date on validation Date of js
  • Combining two different ActiveRecord collections into one
  • bad substitution shell- trying to use variable as name of array
  • wxPython: displaying multiple widgets in same frame
  • Stop Bash Script if Hive Fails
  • C: Incompatible pointer type initializing
  • Google Custom Search with transparent background
  • Why value captured by reference in lambda is broken? [duplicate]
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Obtain ObjectIdHex value from mgo query
  • Volusion's generic SQL folder, functionality
  • How to set/get protobuf's extension field in Go?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • JSON with duplicate key names losing information when parsed
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Convert array of 8 bytes to signed long in C++
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • PHP: When would you need the self:: keyword?
  • Linker errors when using intrinsic function via function pointer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal