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>
#include <stdio.h>
int main( void )
{
struct new
{
int a;
char b;
char c;
} x;
size_t num;
num = (size_t)((struc t new *)&x.c - &x);
printf( "%u\n", num );
return EXIT_SUCCESS;
}
It always returns 0.
Thanks.
--
Alejo 20 6491
Alejo wrote: 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> #include <stdio.h>
int main( void ) { struct new { int a; char b; char c; } x;
size_t num;
num = (size_t)((struc t new *)&x.c - &x);
printf( "%u\n", num );
return EXIT_SUCCESS; }
It always returns 0.
Leave that aside. Why are you doing this anyways? The offset of
members is not a terribly portable quantity. So if this has todo with
loading/saving to a file [or memory buffer] you ought to rethink your
design.
As to the general question, maybe .c is the first element as packed by
the compiler. Try doing
x.c = 4;
and get the assembler code the compiler produces. My compiler [gcc
3.3.1 pre-release] produces
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movb $0, x+5
xorl %eax, %eax
leave
ret
Which seems to be at the end [not start].
Tom
Sorry, I already realised where the mistake is. I should hve done a cast
(char *) instead of (struct new *). Like:
num = (size_t)((char *)&x.c - (char *)&x);
Well, thanks anyway.
Alejo wrote: 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> #include <stdio.h>
int main( void ) { struct new { int a; char b; char c; } x;
size_t num;
num = (size_t)((struc t new *)&x.c - &x);
printf( "%u\n", num );
return EXIT_SUCCESS; }
It always returns 0.
Thanks.
--
Alejo
"Alejo" <al***@chello.n o> wrote in message
news:fKGQa.1661 6$KF1.303303@am stwist00... 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> #include <stdio.h>
int main( void ) { struct new { int a; char b; char c; } x;
size_t num;
num = (size_t)((struc t new *)&x.c - &x);
printf( "%u\n", num );
return EXIT_SUCCESS; }
It always returns 0.
Thanks.
--
Alejo
Hi,
Both(x.c and x) are interpreted as struct new *.
And the difference is less than one struct new: so 0.
This one is working:
num=(size_t)(&x .c-(char*)&x);
Marco
In article <fKGQa.16616$KF 1.303303@amstwi st00>, Alejo <al***@chello.n o>
wrote: Hello,
First, I will _not_ send emails, no matter how much you request them. I
will send emails to you if I intend you to read something that I don't
want the whole world to read, but if I post to the newsgroup then there
is no point in sending an email. You post here, you read here.
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> #include <stdio.h>
int main( void ) { struct new { int a; char b; char c; } x;
size_t num;
num = (size_t)((struc t new *)&x.c - &x);
printf( "%u\n", num );
return EXIT_SUCCESS; }
It always returns 0.
Not suprisingly. If you had an array
struct new array [100];
how many bytes are &array [0] and &array [1] apart? What is the
difference (&array [1]) - (&array [0])? If two pointers p and q are x
byte apart, how does the compiler calculate p - q?
On Tue, 15 Jul 2003 01:09:28 +0200, Alejo <al***@chello.n o> wrote in
comp.lang.c: 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.
Then you shouldn't be posting to comp.lang.c because whatever you
have, it is not a C compiler.
Or perhaps you have neglected to include <stddef.h>?
Understand that C provides for free-standing implementations , for
embedded systems and such, which are not required to provide even one
single function from the standard library, but they are still required
to provide the macros defined in <stddef.h>.
If what you have does not provide a working offsetof() macro in
<stddef.h>, it is literally not a C compiler.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
On Mon, 14 Jul 2003 23:16:15 GMT, Tom St Denis <to********@iah u.ca>
wrote: Alejo wrote: 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> #include <stdio.h>
int main( void ) { struct new { int a; char b; char c; } x;
size_t num;
num = (size_t)((struc t new *)&x.c - &x);
printf( "%u\n", num );
return EXIT_SUCCESS; }
It always returns 0.
Leave that aside. Why are you doing this anyways? The offset of members is not a terribly portable quantity. So if this has todo with loading/saving to a file [or memory buffer] you ought to rethink your design.
As to the general question, maybe .c is the first element as packed by the compiler. Try doing
x.c = 4;
and get the assembler code the compiler produces. My compiler [gcc 3.3.1 pre-release] produces
main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movb $0, x+5 xorl %eax, %eax leave ret
Which seems to be at the end [not start].
If your compiler generates that for x.c = 4, then it is wrong.
Jim
Tom St Denis <to********@iah u.ca> wrote: Alejo wrote: struct new { int a; char b; char c; } x;
As to the general question, maybe .c is the first element as packed by the compiler.
Or maybe not, since it isn't allowed to shuffle elements this way. The
first element of a struct new in memory _must_ be a. There _cannot_ be
any padding before a. Hence, offsetof(struct new, a) must be 0, and
offsetof(struct new, c) is not allowed to be. Moreover, offsetof(struct
new, a) < offsetof(struct new, b) < offsetof(struct new, c).
Richard
On Tue, 15 Jul 2003, Jack Klein wrote: On Tue, 15 Jul 2003 01:09:28 +0200, Alejo wrote in comp.lang.c: 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.
Then you shouldn't be posting to comp.lang.c because whatever you have, it is not a C compiler.
I thought offsetof() was new in C99. Am I mistaken?
-Arthur
Arthur J. O'Dwyer wrote: I thought offsetof() was new in C99. Am I mistaken?
Yes. It's in ANSI C <stddef.h>.
--
Hallvard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
{
|
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)
|
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 */
|
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:
|
by: luke |
last post by:
hi all,
i have another question.
I've read the FAQ regarding my former question (sizeof struct and
union) and in question 2.14 it talks about offset macro.
#define offsetof(type, mem) ((size_t) \
((char *)&((type *)0)->mem - (char *)(type *)0))
Can anyone explain me how it works.
1)I can't understand what "0" casted to (type *) means
| |
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) { ... }
|
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;
};
|
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?.
|
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...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |