Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

C++ offsetof macro

Posted on 2004-11-19
5
Medium Priority
?
2,192 Views
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;
}



0
Comment
Question by:jweston1
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 12622951
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;
}

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

Expert Comment

by:AlexFM
ID: 12622956
This happens because structure members are aligned to 32-bits boundary.
0
 
LVL 13

Accepted Solution

by:
SteH earned 200 total points
ID: 12622957
And have a look at the documentation of
#pragma pack (n)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_predir_pack.asp
to see how to change this behaviour.
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
ID: 12622962
See /Zp compiler option (Struct Member Alignment).
0
 
LVL 3

Author Comment

by:jweston1
ID: 12622977
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

571 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