Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Macro explaination

Macro explaination

Scheduled Pinned Locked Moved C / C++ / MFC
help
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ShankarPS
    wrote on last edited by
    #1

    Can anyone help me explaining what the below macro is supposed to do. #define OFF(type, field) ((LONG)(LONG_PTR)&(((type *)0)->field)) d2hes

    R 1 Reply Last reply
    0
    • S ShankarPS

      Can anyone help me explaining what the below macro is supposed to do. #define OFF(type, field) ((LONG)(LONG_PTR)&(((type *)0)->field)) d2hes

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      It gives the offset of a structure member from the beginning of the structure, in bytes.

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      S 1 Reply Last reply
      0
      • R Ryan Binns

        It gives the offset of a structure member from the beginning of the structure, in bytes.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        S Offline
        S Offline
        ShankarPS
        wrote on last edited by
        #3

        Thanks, but how does it do that?

        R 1 Reply Last reply
        0
        • S ShankarPS

          Thanks, but how does it do that?

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          #define OFF(type, field) ((LONG)(LONG_PTR)&(((type *)0)->field))

          Take the following structure:

          struct ThisStruct
          {
          char field1; // at offset 0
          short field2; // at offset 1
          int field3; // at offset 3
          int field4; // at offset 7
          };

          If we call the macro as such: OFF(ThisStruct, field3) Firstly, the macro creates a pointer to a ThisStruct structure at address 0: ((ThisStruct *)0) Next, it refers to a particular field inside that structure: ((ThisStruct *)0)->field3 Next, it takes the address of that field: &(((ThisStruct *)0)->field3) Since field3 is 3 bytes from the beginning of the structure, and the structure is at address 0, the pointer will hold the value 3. Lastly, the macro converts the pointer to a LONG_PTR and finally to a LONG, to give an integer result - the offset of the field from the beginning of the structure. Hope this helps,

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups