C++ offsetof macro

Posted on 2004-11-19
Last Modified: 2012-05-05
Can someone tell me why the offsetof macro is not returning the correct result? The offset of c is clearly 2 bytes, yet Visual C++ 6.0 returns a value of 4 for the offset. Thanks.

#include "stdafx.h"
#include <windows.h>
#include <stddef.h>
struct a
      short b;  //0x00
      long c;   //0x02

int main(int argc, char* argv[])
      DWORD d = offsetof(a,c);  //0x04
      return 0;

Question by:jweston1
    LVL 13

    Expert Comment

    Due to get variables on predefined memory locations there might be additional bytes added to the struct. For performance reasons variables of length 4 should be on memory locations with the last two bits 0. Try the example again with
    struct a {
      short b1;
      short b2;
      long c;

    offsetof (a, b2) should return 2.
    LVL 48

    Expert Comment

    This happens because structure members are aligned to 32-bits boundary.
    LVL 13

    Accepted Solution

    And have a look at the documentation of
    #pragma pack (n)
    to see how to change this behaviour.
    LVL 48

    Assisted Solution

    See /Zp compiler option (Struct Member Alignment).
    LVL 3

    Author Comment

    It seems the compiler is allocating a long for the short even though I've specified it as short. Maybe this is for alignment or something.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now