473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

FIELD_OFFSET

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
1 4411
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
4
by: nohimn | last post by:
The RegLoadKey function requires that I set both SE_BACKUP_NAME and SE_RESTORE_NAME to enabled. I'm currently trying, but I keep getting an error: Run-Time Check Failure #2 - Stack around the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.