473,811 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Offsetof

Hi,
I have following structures and union

typedef struct
{
int a[100];
int b;
char *c;

}TEST_1;
typedef struct
{
int a[100];
char c[100];

}TEST_2;
union Member
{
TEST_1 test_1;
TEST_2 test_2;
};

typedef enum{
START_ID_0 = 0x2000,
START_ID_1,
START_ID_2,
START_ID_3,
START_ID_4
}SomeIdd;

typedef struct BufferTag
{
SomeId *id;
union Member *mem;
}Buffer;

In my program somehow I get pointer to "mem" in struct "Buffer". Now I
want to get value stored in "id". Is there a way. I read at multiple
places that I can use offsetof but when I try it gives by Segmentation
fault. Can you experts check this and let me know.
FOllowing is what I am doing.

#define GetMemFromBuf (out,source) \
(out) = (Buffer *) \
((char *) (source) - (offsetof(Buffe r, mem)))

Following is that way I am accessing it.
Buffer *myBuff;
GetMemFromBuf (myBuff, (void *)myMem);
printf("ID = %d",(*myBuff->id));

Aug 31 '07 #1
5 1902

<mi*****@gmail. comwrote in message
news:11******** **************@ 22g2000hsm.goog legroups.com...
Hi,
I have following structures and union

typedef struct
{
int a[100];
int b;
char *c;

}TEST_1;
typedef struct
{
int a[100];
char c[100];

}TEST_2;
union Member
{
TEST_1 test_1;
TEST_2 test_2;
};

typedef enum{
START_ID_0 = 0x2000,
START_ID_1,
START_ID_2,
START_ID_3,
START_ID_4
}SomeIdd;

typedef struct BufferTag
{
SomeId *id;
union Member *mem;
}Buffer;

In my program somehow I get pointer to "mem" in struct "Buffer". Now I
want to get value stored in "id". Is there a way. I read at multiple
places that I can use offsetof but when I try it gives by Segmentation
fault. Can you experts check this and let me know.
FOllowing is what I am doing.

#define GetMemFromBuf (out,source) \
(out) = (Buffer *) \
((char *) (source) - (offsetof(Buffe r, mem)))

Following is that way I am accessing it.
Buffer *myBuff;
GetMemFromBuf (myBuff, (void *)myMem);
printf("ID = %d",(*myBuff->id));
Are you sure you get a pointer to mem, and not the value of mem itself?
Ie a union Member **, not a union Member * ?

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 31 '07 #2
On Aug 31, 11:08 am, "Malcolm McLean" <regniz...@btin ternet.com>
wrote:
<mihi...@gmail. comwrote in message

news:11******** **************@ 22g2000hsm.goog legroups.com...
Hi,
I have following structures and union
typedef struct
{
int a[100];
int b;
char *c;
}TEST_1;
typedef struct
{
int a[100];
char c[100];
}TEST_2;
union Member
{
TEST_1 test_1;
TEST_2 test_2;
};
typedef enum{
START_ID_0 = 0x2000,
START_ID_1,
START_ID_2,
START_ID_3,
START_ID_4
}SomeIdd;
typedef struct BufferTag
{
SomeId *id;
union Member *mem;
}Buffer;
In my program somehow I get pointer to "mem" in struct "Buffer". Now I
want to get value stored in "id". Is there a way. I read at multiple
places that I can use offsetof but when I try it gives by Segmentation
fault. Can you experts check this and let me know.
FOllowing is what I am doing.
#define GetMemFromBuf (out,source) \
(out) = (Buffer *) \
((char *) (source) - (offsetof(Buffe r, mem)))
Following is that way I am accessing it.
Buffer *myBuff;
GetMemFromBuf (myBuff, (void *)myMem);
printf("ID = %d",(*myBuff->id));

Are you sure you get a pointer to mem, and not the value of mem itself?
Ie a union Member **, not a union Member * ?

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm- Hide quoted text -

- Show quoted text -
I am getting following:
Buffer received_buff;
void * my_ptr;
my_ptr = (void *)received_buff .mem;
So I have my_ptr, from here I need to get "id".

Hope I am clear.

Aug 31 '07 #3
mi*****@gmail.c om wrote:
On Aug 31, 11:08 am, "Malcolm McLean" <regniz...@btin ternet.com>
wrote:
><mihi...@gmail .comwrote in message

news:11******* *************** @22g2000hsm.goo glegroups.com.. .
>>Hi,
I have following structures and union
typedef struct
{
int a[100];
int b;
char *c;
}TEST_1;
typedef struct
{
int a[100];
char c[100];
}TEST_2;
union Member
{
TEST_1 test_1;
TEST_2 test_2;
};
typedef enum{
START_ID_0 = 0x2000,
START_ID_1,
START_ID_2,
START_ID_3,
START_ID_4
}SomeIdd;
typedef struct BufferTag
{
SomeId *id;
union Member *mem;
}Buffer;
In my program somehow I get pointer to "mem" in struct "Buffer". Now I
want to get value stored in "id". Is there a way. I read at multiple
places that I can use offsetof but when I try it gives by Segmentation
fault. Can you experts check this and let me know.
FOllowing is what I am doing.
#define GetMemFromBuf (out,source) \
(out) = (Buffer *) \
((char *) (source) - (offsetof(Buffe r, mem)))
Following is that way I am accessing it.
Buffer *myBuff;
GetMemFromBuf (myBuff, (void *)myMem);
printf("ID = %d",(*myBuff->id));
Are you sure you get a pointer to mem, and not the value of mem itself?
Ie a union Member **, not a union Member * ?

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm- Hide quoted text -

- Show quoted text -

I am getting following:
Buffer received_buff;
void * my_ptr;
my_ptr = (void *)received_buff .mem;
So I have my_ptr, from here I need to get "id".

Hope I am clear.
It is clear from the declarations you just posted. receiver.mem is a
pointer. You code, shown above, has not initialized it, so my_ptr is
set to an uninitialized value. However, you want to use a pointer to
received_buff.m em, so
my_ptr = &received_buff. mem;

then
Buffer *myBuff;
GetMemFromBuf (myBuff, (my_ptr);
printf("ID = %d",(myBuff->id));

Note the two changes made to your code. This assumes that
received_buff.i d has been initialized.

--
Thad
Sep 1 '07 #4
On Fri, 31 Aug 2007 16:24:04 -0000, mi*****@gmail.c om wrote:
>On Aug 31, 11:08 am, "Malcolm McLean" <regniz...@btin ternet.com>
wrote:
><mihi...@gmail .comwrote in message

news:11******* *************** @22g2000hsm.goo glegroups.com.. .
Hi,
I have following structures and union
typedef struct
{
int a[100];
int b;
char *c;
}TEST_1;
typedef struct
{
int a[100];
char c[100];
}TEST_2;
union Member
{
TEST_1 test_1;
TEST_2 test_2;
};
typedef enum{
START_ID_0 = 0x2000,
START_ID_1,
START_ID_2,
START_ID_3,
START_ID_4
}SomeIdd;
typedef struct BufferTag
{
SomeId *id;
union Member *mem;
}Buffer;
In my program somehow I get pointer to "mem" in struct "Buffer". Now I
No you don't. See the comments below.
want to get value stored in "id". Is there a way. I read at multiple
places that I can use offsetof but when I try it gives by Segmentation
fault. Can you experts check this and let me know.
FOllowing is what I am doing.
#define GetMemFromBuf (out,source) \
(out) = (Buffer *) \
((char *) (source) - (offsetof(Buffe r, mem)))
The apparent purpose of this macro is to find the start of a
particular Buffer structure given the address of the member mem in
that particular structure.
>>
Following is that way I am accessing it.
Buffer *myBuff;
GetMemFromBuf (myBuff, (void *)myMem);
What is myMem? What do you think the cast is doing?

At this point in time, you do not have any object of type Buffer. You
do have a pointer but it doesn't point anywhere until the macro
expansion assigns it a value. If you insert the following before the
macro call
Buffer b;
then the macro call
GetMemFromBuf (myBuff, &b.mem);
will work and myBuff will point to b.
printf("ID = %d",(*myBuff->id));
The code invokes undefined behavior because you never initialized id
to contain the address of an enum object (possibly because you never
had an actual struct).
>>
Are you sure you get a pointer to mem, and not the value of mem itself?
Ie a union Member **, not a union Member * ?
I am getting following:
Buffer received_buff;
void * my_ptr;
my_ptr = (void *)received_buff .mem;
The cast is unnecessary and the code invokes undefined behavior
because mem has never been initialized with or assigned a value.
>

So I have my_ptr, from here I need to get "id".
You have my_ptr but it does not contain the address of mem. Malcolm's
question hits the nail on the head.

If you want the address of mem, then use
my_ptr = &received_buff. mem;
Remove del for email
Sep 1 '07 #5
On Fri, 31 Aug 2007 21:54:11 -0600, Thad Smith <Th*******@acm. org>
wrote:
>mi*****@gmail. com wrote:
>On Aug 31, 11:08 am, "Malcolm McLean" <regniz...@btin ternet.com>
wrote:
>><mihi...@gmai l.comwrote in message

news:11****** *************** *@22g2000hsm.go oglegroups.com. ..

Hi,
I have following structures and union
typedef struct
{
int a[100];
int b;
char *c;
}TEST_1;
typedef struct
{
int a[100];
char c[100];
}TEST_2;
union Member
{
TEST_1 test_1;
TEST_2 test_2;
};
typedef enum{
START_ID_0 = 0x2000,
START_ID_1,
START_ID_2,
START_ID_3,
START_ID_4
}SomeIdd;
typedef struct BufferTag
{
SomeId *id;
union Member *mem;
}Buffer;
In my program somehow I get pointer to "mem" in struct "Buffer". Now I
want to get value stored in "id". Is there a way. I read at multiple
places that I can use offsetof but when I try it gives by Segmentation
fault. Can you experts check this and let me know.
FOllowing is what I am doing.
#define GetMemFromBuf (out,source) \
(out) = (Buffer *) \
((char *) (source) - (offsetof(Buffe r, mem)))
Following is that way I am accessing it.
Buffer *myBuff;
GetMemFromBuf (myBuff, (void *)myMem);
printf("ID = %d",(*myBuff->id));
Are you sure you get a pointer to mem, and not the value of mem itself?
Ie a union Member **, not a union Member * ?

--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm- Hide quoted text -

- Show quoted text -

I am getting following:
Buffer received_buff;
void * my_ptr;
my_ptr = (void *)received_buff .mem;
So I have my_ptr, from here I need to get "id".

Hope I am clear.

It is clear from the declarations you just posted. receiver.mem is a
pointer. You code, shown above, has not initialized it, so my_ptr is
set to an uninitialized value. However, you want to use a pointer to
received_buff. mem, so
my_ptr = &received_buff. mem;

then
Buffer *myBuff;
GetMemFromBuf (myBuff, (my_ptr);
printf("ID = %d",(myBuff->id));

Note the two changes made to your code. This assumes that
received_buff. id has been initialized.
The member id is a pointer. To print the value it points to with %d,
you need to dereference it as his original code did. If you want to
print the address it points to, you need to use %p and cast it to
void*.
Remove del for email
Sep 2 '07 #6

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

Similar topics

5
6348
by: Hiroki Horiuchi | last post by:
Hello. I wrote a program, but g++ warns a.c:11: warning: invalid access to non-static data member `A::y' of NULL object a.c:11: warning: (perhaps the `offsetof' macro was used incorrectly) The program is like below. class A {
9
2970
by: Exits Funnel | last post by:
Consider this code which is a very trimmed down version of some I've inherited and am trying to port from windows to g++: //Begin test1.cpp class foo { int i; int j; }; class bar { bar (int foo::* dataMember) :offsetof (foo, *dataMember) //Call this Line (A)
20
6491
by: Alejo | last post by:
Hello, My implementation does not define offsetof, so I have designed a little program that 'attempts' to find the relative position of a member in its structure. It just does not work. Could I get some pointers on what I am doing wrong (apart from being coding so late at night). #include <stdlib.h>
6
2205
by: Arthur J. O'Dwyer | last post by:
As far as I know, C89/C90 did not contain the now-standard offsetof() macro. Did C89 mandate that structs had to have a consistent layout? For example, consider the typical layout of the following structure: struct weird { int x; /* sizeof(int)==4 here */
10
3416
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with <offset>). What I would like to do is create an API something like this: #include <stddef.h> struct MemMap { unsigned char apple; // 8 bits on my platform
44
3761
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still doesn't make sense to me. The sticking point seems to be:
7
4540
by: Fred Zwarts | last post by:
Consider the following definition: typedef struct { int a; int b; } s; Now I have a function void f (int i) { ... }
8
5870
by: Pawel | last post by:
Hallo group members. //p1.cpp #include <stdio.h> #include <linux/stddef.h> struct Person { int m_age; char* m_name; };
11
2435
by: Kavya | last post by:
offsetof(T,m) (size_t)&(((T*)0)->m) Why do we always start from 0 in this macro to access the offset of structure or union. Does standard guarantees that structure and union reside at address 0? If yes, then what if I have two or more structures. How can they reside at same address?.
2
3931
by: Imre | last post by:
Hi I know that offsetof is basically a C leftover, and only works for POD types, not classes, so it is recommended that I use pointers to members instead. However, I have a problem where I don't see how I should use pointers to members. Basically, I know how to get a member if I have an object and a pointer-to-member (obj.*ptr instead of obj + offset), but I don't know how to do the opposite: getting the object from a member address and...
0
10656
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...
1
10410
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
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
9214
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
7674
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
6897
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
4353
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
3878
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3027
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.