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

c structure

51
Hi all,
I read the below question in a blog, and I am not sure if a portable way to do the same exists, or even a way to do this in C.

A function accepts an address of a structure member (not necessarily the first member) as its only argument. Given this is there a way to find out the start address of this structure object in memory. Assuming the hardware supports non aligned reads (implementation not important) and the compiler never pads the alignment bytes.

Please advice.

Thanks,
Gsi.
Mar 14 '08 #1
9 1601
Banfa
9,065 Expert Mod 8TB
Do you mean a pointer to any member of any structure or a pointer to a specific member of a specific structure?

In the first case there is no way to do it (that I can think of), in the second case then there are some no portable methods that would work (on some platforms).
Mar 14 '08 #2
JosAH
11,448 Expert 8TB
Do you mean a pointer to any member of any structure or a pointer to a specific member of a specific structure?

In the first case there is no way to do it (that I can think of), in the second case then there are some no portable methods that would work (on some platforms).
There's even a portable way to figure that out. There's the offsetof() macro in
<stddef.h>

kind regards,

Jos
Mar 14 '08 #3
Banfa
9,065 Expert Mod 8TB
There's even a portable way to figure that out. There's the offsetof() macro in
<stddef.h>
Blimey your right but ...

I don't see how it can be there and be necessarily possible to implement on all conforming platforms. For instance in MSVC 2005 this is implemented as

#define offsetof(s,m) (size_t)&(((s *)0)->m)

But I would say that that was dereference on a NULL pointer and thus invoking undefined behaviour and that the cast from pointer to size_t has (at the very most) implementation defined behaviour.

And therefore an implementation could define there behaviours such that constructs like this do not work making the calculation impossible.
Mar 14 '08 #4
JosAH
11,448 Expert 8TB
Blimey your right but ...

I don't see how it can be there and be necessarily possible to implement on all conforming platforms. For instance in MSVC 2005 this is implemented as

#define offsetof(s,m) (size_t)&(((s *)0)->m)

But I would say that that was dereference on a NULL pointer and thus invoking undefined behaviour and that the cast from pointer to size_t has (at the very most) implementation defined behaviour.

And therefore an implementation could define there behaviours such that constructs like this do not work making the calculation impossible.
No, taking the address of a member of something doesn't involve any dereference.
Just simple address calculations can do the job. On systems where the null
pointer doesn't yield 0 (all bits zero) this macro doesn't do it of course so never
use this little gibberish yourself as portable code, i.e. use the macro instead.

kind regards,

Jos
Mar 14 '08 #5
gsi
51
Hi all,
Yes I was referring to what banfa said at the first place, a pointer to any member to any structure and thanks for the response.


No, taking the address of a member of something doesn't involve any dereference.
Just simple address calculations can do the job. On systems where the null
pointer doesn't yield 0 (all bits zero) this macro doesn't do it of course so never
use this little gibberish yourself as portable code, i.e. use the macro instead.

kind regards,

Jos
Just curious, If the null pointer in an implementation is not all bits zero, is it that the same is achieved via pointer arithmetic (may be the difference between the member and the start of structure, assuming machine is byte addressable and pointer converted to a char* beforehand).

Thanks in advance,
Gsi.
Mar 14 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
Just curious, If the null pointer in an implementation is not all bits zero,
What implementation does this?

If you have such a thing, it belongs in File 13.

BTW Jos: Does your nifty offsetof take into account vtbl pointers?
Mar 14 '08 #7
gsi
51
What implementation does this?
If you have such a thing, it belongs in File 13.

Diab Coldfire compiler
#define offsetof(s,memb) ((size_t)((char *)&((s *)0)->memb-(char *)0))

Didn't look at the compiler documentation though, I am assuming the null pointer for this implementation is not all bits zero. Please advice.

Thanks,
Gsi.
Mar 14 '08 #8
JosAH
11,448 Expert 8TB
What implementation does this?

If you have such a thing, it belongs in File 13.

BTW Jos: Does your nifty offsetof take into account vtbl pointers?
I had to look it up: Bjarne Stroustrup, the C++ programming language III, p.419:
you have to use the X::* syntax for a type X which isn't surprising because a
(virtual) method isn't 'physically' part of an object.

kind regards,

Jos

ps. I've never seen an architecture either where NULL wasn't a real all bits zero
address except for those silly 'small' models on PCs (the igored segment value
could be non-zero).
Mar 14 '08 #9
gsi
51
Hi,


BTW Jos: Does your nifty offsetof take into account vtbl pointers?

I suppose the offsetof() function macro extended to c++ is restricted only for "pod types" in which classes, more or less corresponds to the C concept of struct . I guess the previous versions of g++ did not have a c++ compliant macro defined, which pulled in the C compliant macro from <stddef.h> resulting in compile time errors. Recent g++ has it specialized for c++(using conditional preprocessors) but that too resticted for pod types.

Thanks,
Gsi.
Mar 14 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
4
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
4
by: eBob.com | last post by:
In my class which contains the code for my worker thread I have ... Public MustInherit Class Base_Miner #Region " Delegates for accessing main UI form " Delegate Sub DelegAddProgressBar(ByVal...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...
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
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...

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.