473,800 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

g++ "offsetof" problem

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
{
private:
int x;
int y;
public:
static unsigned int const y_offset;
};

unsigned int const A::y_offset = (
reinterpret_cas t<char *>(&static_cast <A *>(0)->y)
-
static_cast<cha r *>(0)
);

I want to define a constant like A::y_offset with g++ without warnings.
Is it possible?

Thank you in advance.

//Hiroki Horiuchi
Jul 22 '05 #1
5 6347
Hiroki Horiuchi wrote:
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
{
private:
int x;
int y;
public:
static unsigned int const y_offset;
};

unsigned int const A::y_offset = (
reinterpret_cas t<char *>(&static_cast <A *>(0)->y)
-
static_cast<cha r *>(0)
);

I want to define a constant like A::y_offset with g++ without
warnings.
Is it possible?


Nope. offsetof is only valid for C structs. You will need to rethink your
design.

--
Attila aka WW
Jul 22 '05 #2
Hiroki Horiuchi wrote in
news:85******** *************** ***@posting.goo gle.com:
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
{
private:
int x;
int y;
public:
static unsigned int const y_offset;
};

unsigned int const A::y_offset = (
reinterpret_cas t<char *>(&static_cast <A *>(0)->y)
-
static_cast<cha r *>(0)
);
static_cast<A *>(0) is NULL pointer you may not dereference it any
portable way. Note the Standard says that the inbult operator ->
derefences the pointer that its applied to, so it does even though
all you do is take an address.

I want to define a constant like A::y_offset with g++ without
warnings. Is it possible?


Look into the offsetof macro and use it correctly,

#include <iostream>
#include <cstdlib>

/* struct/class must be a POD (Plain Old Data) to be
sutable for use with offsetof
*/
struct A_POD
{
int x;
int y;
int z;
};

class A : private A_POD
{
public:

int &operator [] ( unsigned off );

static unsigned int const offset[ 3 ];

A( int xx, int yy, int zz )
{
x = xx;
y = yy;
z = zz;
}
};

unsigned int const A::offset[ 3 ] =
{
offsetof( A_POD, x ),
offsetof( A_POD, y ),
offsetof( A_POD, z )
};
int & A::operator [] ( unsigned off )
{
return *reinterpret_ca st< int * >(
reinterpret_cas t< char * >(
static_cast< A_POD * >( this )
)
+
offset[ off ]
);
}
int main()
{
A a( 1, 2, 3 );

std::cerr
<< a[ 0 ] << "\n"
<< a[ 1 ] << "\n"
<< a[ 2 ] << "\n"
;
}

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
On 25 Nov 2003 04:48:40 -0800, hi****@air.ne.j p (Hiroki Horiuchi)
wrote:
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
{
private:
int x;
int y;
public:
static unsigned int const y_offset;
};

unsigned int const A::y_offset = (
reinterpret_cas t<char *>(&static_cast <A *>(0)->y)
-
static_cast<cha r *>(0)
);

I want to define a constant like A::y_offset with g++ without warnings.
Is it possible?


Possibly, but it isn't portable anyway. offsetof can only be used with
POD types (which can't have private data). This compiles without
warnings:

#include <stddef.h>

class A
{
public:
int x;
int y;
static unsigned int const y_offset;
};

unsigned int const A::y_offset = offsetof(A, y);

Tom
Jul 22 '05 #4

"Hiroki Horiuchi" <hi****@air.ne. jp> wrote in message news:85******** *************** ***@posting.goo gle.com...
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)

It's undefiend to use offsetof on non-POD's.
Jul 22 '05 #5
Hiroki Horiuchi wrote:
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
{
private:
int x;
int y;
public:
static unsigned int const y_offset;
};

unsigned int const A::y_offset = (
reinterpret_cas t<char *>(&static_cast <A *>(0)->y)
-
static_cast<cha r *>(0)
);

I want to define a constant like A::y_offset with g++ without warnings.
Is it possible?

Thank you in advance.

//Hiroki Horiuchi


You can't get the "offset" of it, but you could do something like this:

class A
{
private:
int x;
int y;
public:
static int A::* y_offset;
}

int A::* A::y_offset = &A::y;
Jul 22 '05 #6

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

Similar topics

10
8316
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b) This way I have to write another "serializing" function for every new kind of struct I want to write, though. Is there a way to write functions that can write/read any struct to/from plain text format in a portable way?
8
3378
by: Chul Min Kim | last post by:
Hi, I got a BUS ERROR from one of my company's program. Let me briefly tell our environment. Machine : Sun E3500 (Ultra Sparc II 400Mhz CPU 4EA) OS : Solaris7 Compiler : Sun Workshop 5.0 cc complier I get "BUS ERROR" only when I execute the binary which builds
188
17462
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
26
3005
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier interupted before it could write the variables to the file, the file is gonna be empty, and then it's gonna load a load of crap into variables, which i want to avoid. That file is always 36 bytes big (it contains 4 double-precision floats and one...
2
2179
by: Chris Thomasson | last post by:
I was wondering if the 'SLINK_*' and 'SLIST_*' macros, which implement a simple singly-linked list, will produce _any_ possible undefined behavior: ____________________________ #include <stdio.h> #include <stdlib.h> #include <assert.h>
4
1594
by: Julek | last post by:
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; };
2
2177
by: LittleG | last post by:
I have a huge type and I want to make a command that will give an offset and get the member. I can't make a table. I'm looking for a macro or an idea of how to make one. Thanks LittleG
0
9690
marktang
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...
0
10274
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...
1
7576
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
6811
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();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
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.