FIELD_OFFSET

H

harry

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
 
T

Thomas Matthews

harry said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,319
Latest member
LorenFlann

Latest Threads

Top