473,320 Members | 1,955 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,320 software developers and data experts.

The "position" of variables

Hi,
I wanted to know, if there is guarantee that a specific variable is
always the same number of bytes forward than the beginning of the
struct/class. Example:

class MyClass
{
....
int var;
};
....
// somewhere we create one object
MyClass obj;
....
// somewhere else we create other object
MyClass *obj2=new MyClass;
....
// check the distance from the beginning of structure
int diff1=int( ((char*)&obj.var) - ((char*)&obj) );
int diff2=int( ((char*)&obj2->var) - ((char*)obj2) );
if(diff1 == diff2 )
{
// it's the same distance
}

Can I always assume, that diff1 == diff2, no matter how MyClass
structures are created? Even if they have multiple inheritance and
other "advanced" features?

Specifically, I want to know the answer for Visual C++ 2003, and
SunStudio.

Thanks in advance,
Julek
Jun 27 '08 #1
4 1561
On 2008-05-29 08:21:32 -0400, Julek <ju******@go2.plsaid:
Hi,
I wanted to know, if there is guarantee that a specific variable is
always the same number of bytes forward than the beginning of the
struct/class. Example:

class MyClass
{
...
int var;
};
...
// somewhere we create one object
MyClass obj;
...
// somewhere else we create other object
MyClass *obj2=new MyClass;
...
// check the distance from the beginning of structure
int diff1=int( ((char*)&obj.var) - ((char*)&obj) );
int diff2=int( ((char*)&obj2->var) - ((char*)obj2) );
if(diff1 == diff2 )
{
// it's the same distance
}

Can I always assume, that diff1 == diff2, no matter how MyClass
structures are created? Even if they have multiple inheritance and
other "advanced" features?
Yes. But note that this is a somewhat constrained question: var is a
direct member of MyClass. More generally, no, you can't assume that any
member of a class will always be at the same offset. In particular,
virtual base classes (and, hence, their members) can move around,
depending on how inheritance is used.

Formally, the calculation of diff1 and diff2 is equivalent to the macro
offsetof, which can only be applied to POD types. The more general way
of pointing at a selected member of an arbitrary object of type T is
with a pointer to member of T:

int MyClass::*ptr = &MyClass::var;
MyClass obj1;
obj1.*ptr = 3; // assigns to obj1.var
MyClass obj2;
obj2.*ptr = 4; // assigns to obj2.var

If you have a pointer instead of an object or a reference, use ->* to
get at the member:

MyClass *pmc = &obj1;
int i = pmc->*ptr; // copies from obj1.var

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #2
On 2008-05-29 14:36, Pete Becker wrote:
On 2008-05-29 08:21:32 -0400, Julek <ju******@go2.plsaid:
>Hi,
I wanted to know, if there is guarantee that a specific variable is
always the same number of bytes forward than the beginning of the
struct/class. Example:

class MyClass
{
...
int var;
};
...
// somewhere we create one object
MyClass obj;
...
// somewhere else we create other object
MyClass *obj2=new MyClass;
...
// check the distance from the beginning of structure
int diff1=int( ((char*)&obj.var) - ((char*)&obj) );
int diff2=int( ((char*)&obj2->var) - ((char*)obj2) );
if(diff1 == diff2 )
{
// it's the same distance
}

Can I always assume, that diff1 == diff2, no matter how MyClass
structures are created? Even if they have multiple inheritance and
other "advanced" features?

Yes. But note that this is a somewhat constrained question: var is a
direct member of MyClass. More generally, no, you can't assume that any
member of a class will always be at the same offset. In particular,
virtual base classes (and, hence, their members) can move around,
depending on how inheritance is used.
But surely you can assume that all instances of a class/struct in a
running program have the same layout. Of course of you recompile the
program they layout can change but in each instance of a program it
should be well-defined.

--
Erik Wikström
Jun 27 '08 #3
On 2008-05-29 12:33:55 -0400, Erik Wikström <Er***********@telia.comsaid:
>
But surely you can assume that all instances of a class/struct in a
running program have the same layout. Of course of you recompile the
program they layout can change but in each instance of a program it
should be well-defined.
No, you can't. Virtual bases aren't always at the same offset relative
to any particular derived class.

struct base
{
int i;
};

struct i1 : virtual base
{
int j;
};

struct i2 : virtual base
{
int k;
};

struct der : i1, i2
{
int m;
};

Let's say that, for an object of type i1, the compiler puts the base
subobject immediately after the direct members of i1, and for an object
of type i2, it puts the base subobject immediately after the direct
members of i2. Now where does that base subobject end up when you have
an object of type der? If it's immediately after the direct members of
i1, then it can't be immediately after the direct members of i2, so the
offset of i from the i2 subobject is different from the offset in a
standalone i2 object. And vice versa. So one of the offsets has to
change.

This is typically implemented by putting a pointer to base in i1 and in
i2. To get at i, the compiler generates code that follows the pointer.
This isn't really the place for a more detailed explanation. Lipman's
"Inside the C++ Object Model" goes through all the details of a typical
implementation.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #4
On 2008-05-29 20:45, Pete Becker wrote:
On 2008-05-29 12:33:55 -0400, Erik Wikström <Er***********@telia.comsaid:
>>
But surely you can assume that all instances of a class/struct in a
running program have the same layout. Of course of you recompile the
program they layout can change but in each instance of a program it
should be well-defined.

No, you can't. Virtual bases aren't always at the same offset relative
to any particular derived class.

struct base
{
int i;
};

struct i1 : virtual base
{
int j;
};

struct i2 : virtual base
{
int k;
};

struct der : i1, i2
{
int m;
};

Let's say that, for an object of type i1, the compiler puts the base
subobject immediately after the direct members of i1, and for an object
of type i2, it puts the base subobject immediately after the direct
members of i2. Now where does that base subobject end up when you have
an object of type der? If it's immediately after the direct members of
i1, then it can't be immediately after the direct members of i2, so the
offset of i from the i2 subobject is different from the offset in a
standalone i2 object. And vice versa. So one of the offsets has to
change.
Yes, of course you can never make any assumptions about the offset of
member in one class based on the offset in another class. But the offset
of a member in different instances of the same class (which is what the
OP asked about) should be well defined.

--
Erik Wikström
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Bordner | last post by:
Hi All, Given the following XML, I've been trying to figure out what XPATH expression will give me the "position" of a <lineitem> within the <shipto> that is its grandparent. To give you an...
1
by: nobody | last post by:
hi there! given <!ELEMENT a (#PCDATA | x)*> <!ELEMENT x (#PCDATA)> how can I find out if x is "embedded" at the beginning <a><x>xxx</x>aaa</a> or at the end <a>aaa<x>xxx</x></a> or in the...
0
by: Luigi | last post by:
I would know what "criteria" is used in box positioning with the position property. Here my tries (using IE and Opera): I have a box with an absolute positioning (properties used: position,...
2
by: Jessard | last post by:
Hi All, I am writing a .NET app that accesses an access View. The view has a table column called 'Position'. When i try and right an UpdateCommand string for this using this column I am...
4
by: Ghyslaine Crespe | last post by:
Hello, In my script, the line document.getElementById(id).style.background-position = "-200px -500px"; fails ! So, how can I change the background-position value ?
1
by: tschoepi | last post by:
Hi there, if I try to access a table named Position in an Access-Database via OleDbDataAdapter I get an error. If I rename the table to e.g. Positions it works. Any idea how to solve this...
2
by: petermichaux | last post by:
Hi, I tried the following and everything worked fine. element.style.position="relative"; Then I tried to make the CSS rule important and it didn't work. The positioning was all wrong in...
3
by: rsrimantula | last post by:
I have a table with html code <style> div.scroll { height:150px; overflow:auto; } table.scrollable th { position:relative;
1
by: vunet.us | last post by:
Mozilla reported the fix to this bug: https://bugzilla.mozilla.org/show_bug.cgi?id=167801. When input text field is located over div, the cursor cannot be seen unless special CSS properties are...
12
by: hiro | last post by:
Hi there, I have a 2 lists.. for simplicities sake lets say the are: l1 = l2 = what I need to do is compare l1 against l2 and return the "position" of where each object in l1 is in l2 ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.