473,659 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.va r) - ((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 1584
On 2008-05-29 08:21:32 -0400, Julek <ju******@go2.p lsaid:
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.va r) - ((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::va r;
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.p lsaid:
>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.va r) - ((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
5158
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 idea of what I'm trying to do...I'm transforming this XML into XSL-FO; which I will process with FOP to produce a PDF file. Each <shipto> element with the XML correlates to a new "section"
1
2606
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 middle
0
1527
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, left and top). I noted that if the reference of this box is the "html" box or the "body" box, the browser uses the following rules:
2
1776
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 greeted with a 'Syntax error in Update statement.' when attempting the update. I take that field out and the update works. I am assuming Position is a reserved word. I have even tried changing the name of the column in the View to something other...
4
26580
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
1449
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 problem? (There are about 5.000 databases out there at our customers with a table in it named Position!) Thanks a lot for your help
2
5083
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 Safari and very jerky in Firefox. element.style.position="relative !important";
3
3000
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
5100
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 applied. The link above illustrates many example. The problem I came across with, is almost identical, except that I need to use position:fixed for my div element instead of position:absolute as in all examples illustrated on Mozilla link. It is...
12
5851
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 ie: pos = 0, 2, 4
0
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.