By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

FIELD_OFFSET

P: n/a
Hi ppl

I have the following code snippet:

#include "stdafx.h"
#include "conio.h"
#include "malloc.h"
#include <stdio.h>
#include <stddef.h>

struct base
{
char c;
int i;
};

struct der
{
base b;
};

void FillUp(der *p)
{
int temp1=offsetof(struct der,b);
int temp2=offsetof(struct base,i);
//LINE 3
printf("\ntemp1 is:%d",temp1); //gives 0
//LINE 4
printf("\ntemp2 is:%d",temp2); //gives 4
int *ptr;
ptr=(int*)p+temp1/sizeof(base)+temp2/sizeof(int);
printf("\nInside Fillup: 0x%08X",ptr); //gives 0012fed4
*ptr=10;
}

int _tmain(int argc, _TCHAR* argv[])
{
der d;
int temp1=offsetof(struct der,b);
int temp2=offsetof(struct base,i);
int *ptr;
// LINE 1
printf("\ntemp1 is:%d",temp1); //gives 0
//LINE 2
printf("\ntemp2 is:%d",temp2); //gives 4
ptr=(int*)&d+temp1/sizeof(base)+temp2/sizeof(int);
printf("\nInside main: 0x%08X",ptr); //gives 0012fed4
FillUp(&d);
printf("\nnormal way is: 0x%08X",&d.b.i);
printf("\n%d",d.b.i); //gives 10
getch();
return 0;
}

The code snippet is fully working. But I have a small problem when I am
using the above architecture in a bigger project.

LINE 1 gives 0
LINE 2 gives value x
LINE 3 gives 0
LINE 4 gives value y

When can be the case that there is a conflict and line 2 and line 4
gives different values?? How is is possible that the offset that has
been calculated for same member for same structure is different in 2
cases??

Any help would be priceless....I am totally foxed!!!

Thanks very much
regards
HARRY

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
harry wrote:
Hi ppl

I have the following code snippet:

#include "stdafx.h"
#include "conio.h"
#include "malloc.h"
#include <stdio.h>
#include <stddef.h>

struct base
{
char c;
int i;
};

struct der
{
base b;
}; [snip]
Any help would be priceless....I am totally foxed!!! Simply stated: Padding.

The compiler is allowed to add padding bytes between
members of structures and classes. The compiler is
also allowed to add padding between parent members
and child members.

There is no standard as to how many padding bytes
can be inserted.

Don't use offsets.
Don't use memcpy.
Access members directly.
Don't use structures or classes to model real
world data structures on a 1:1 basis. Instead,
load the real world data into a buffer, then
load a structure, field by field, from the data.
Similarly with outputting of data.

Thanks very much
regards
HARRY

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.