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

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_cast<char *>(&static_cast<A *>(0)->y)
-
static_cast<char *>(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 6306
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_cast<char *>(&static_cast<A *>(0)->y)
-
static_cast<char *>(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.google.c om:
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_cast<char *>(&static_cast<A *>(0)->y)
-
static_cast<char *>(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_cast< int * >(
reinterpret_cast< 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.jp (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_cast<char *>(&static_cast<A *>(0)->y)
-
static_cast<char *>(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.google.c om...
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_cast<char *>(&static_cast<A *>(0)->y)
-
static_cast<char *>(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
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)...
8
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...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
26
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...
2
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...
4
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.