public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* invalid offsetof from non-POD type
@ 2003-04-21 13:49 John Quigley
  2003-04-21 14:30 ` Gabriel Dos Reis
  2003-04-21 14:50 ` Nathan Sidwell
  0 siblings, 2 replies; 38+ messages in thread
From: John Quigley @ 2003-04-21 13:49 UTC (permalink / raw)
  To: gcc

The code below triggers an "invalid offsetof from non-POD type" warning:

In this case, and in every case I've seen, the warning is a false alarm and 
the compiler produces the correct result.  Is there a case where the warning 
fires and the compiler produces an incorrect result?  (i.e.: returns the 
wrong offset)?  If there is no such case, is there anything wrong with 
providing a command line flag in gcc that allows the user to disable this 
warning?

Thanks,
- jq

#include <cstddef>
#include <iostream>
using namespace std;

class Foo
{
   public:
      int x;
      char fillerdata[256];
      int y;

      Foo()
      {
         x = 0;
         y = 0;
      }
};

int main()
{
   int yoffset = offsetof(Foo, y); // triggers warning
   cout << "yoffset: " << yoffset << endl;

   Foo foo;
   
   // get a pointer to y using the computed offset
   int* yptr = (int*)(((unsigned char*)&foo) + yoffset);
   // test it be assigning 
   *yptr = 15;
   cout << "foo values: " << foo.x << ", " << foo.y << endl;

   return 0;
}

^ permalink raw reply	[flat|nested] 38+ messages in thread
* Re: invalid offsetof from non-POD type
@ 2003-09-12  4:32 Arnold Cole
  2003-09-12  4:55 ` Gabriel Dos Reis
  0 siblings, 1 reply; 38+ messages in thread
From: Arnold Cole @ 2003-09-12  4:32 UTC (permalink / raw)
  To: gcc

This is regarding a thread from Apr 2003. At that time
someone complained against this warning issued by the
C compiler.

Apparently the standard mandates that 'offsets may
only be applied to POD types. [18.1]/5'

While this may be so, it doesn't solve my coding
problem and doesn't explain to me why my class, with a
user-defined constructor (hence not a POD - whatever
this is) may not be used in offsetof. In my mind,
offsetof stands just on the same level as sizeof.
There are legitimate uses for them both, regardless of
user defined constructor.

While gcc 3.2 issues the warning, I figured a way to
redefine the macro such that it doesn't complain
anymore. Here it is for you guys out there to use it,
until we all agree whether offsetof is good or bad to
use.

#define offsetof(cls,member) (((size_t)&((cls
*)0x100->member)-0x100)

The trick is to use 0x100 (or any other value) instead
of 0. I used 0x100 to avoid any potential alignment 
issues.

If any of the standard gurus out there would care to
explain to me why offsetof shouldn't work for non POD
types (and what POD types are), I'd appreciate it.

All the best!
Adrian



__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

^ permalink raw reply	[flat|nested] 38+ messages in thread
* Re: invalid offsetof from non-POD type
@ 2003-08-12 13:38 skaller
  2003-08-12 14:36 ` Fergus Henderson
  0 siblings, 1 reply; 38+ messages in thread
From: skaller @ 2003-08-12 13:38 UTC (permalink / raw)
  To: gcc

This email refers to an archived thread on this topic.
My question is whether some action has been taken with
respect to this issue.

My understanding is:

1. g++ is entitled to issue a warning on non-standard use
use of offsetof. However, g++ 3.2.2 also issues the same
diagnostic for the calculation

	(unsigned char*)(X*)0 - (unsigned char*)(void*)
		(&(((X*)0)->x))

which it is very misleading. If this calculation is invalid,
the fault lies entirely in the subexpression

	((X*)0)->x

which is probably undefined even if X is a POD.

2. The recommendation to use 'pointers to members' instead
should be removed. The pointer to member semantics in C++
are incomplete and inadequate for certain tasks where
an integral offset is required, for example by a lower
level memory management routine.

3. At least in g++ 3.2.2 there seems to be no way
to turn off this warning individually: I am forced
to use -w to turn off all warnings, which makes me
very uncomfortable. (Have I missed something?)

4. The rule with offsetof was originally formulated as
is to (a) ensure C compatibility and (b) otherwise
provide implementors with the maximum freedom.

Unfortunately, the rule is grossly overzealous,
and a change in the Standard must be considered necessary.

An ambitious change would involve fixing pointers to members
so they might be adequate for tasks currently performed using
integers, memory pointers and casts. A less ambitious change
would relax the requirements so that provided the member
was accessible and the access path from the structure unambiguous
and not crossing a virtual base boundary, the offsetof() macro
should have a well defined result, being the number of bytes
offset the member is from the start of the designated structure.

In the meantime however, I think g++ would best serve the
community by continuing to issue a warning by default,
a hard error if enough strictness options are enabled,
and neither an error nor diagnostic if a specific switch
is set: it seems likely g++ already generates the correct code
in cases it warns about.

My particular product generates a large number of data structures
which are intended to be collected by an exact garbage collector,
and in order for the collector to operate it must know the location
of all pointers in the system. My tool generates tables containing
this data, but it must use the offsetof() macro to do so.

The only alternative I have is to bypass C++ completely and
layout all data structures manually -- forgoing any real opportunity
for C/C++ interoperability, which is one of the products key features.

I *cannot* live with the warning. It is out of the question for
a compiler to systematically generate a large number of these
spurious warnings, which it is guarranteed to do for every
piece of code my tool generates.


^ permalink raw reply	[flat|nested] 38+ messages in thread
* invalid offsetof from non-POD type
@ 2003-01-10  7:28 Chris Croswhite
  0 siblings, 0 replies; 38+ messages in thread
From: Chris Croswhite @ 2003-01-10  7:28 UTC (permalink / raw)
  To: gcc

Hello all,

Perhaps someone could give me had with understanding this error:
"foo.c:15: warning: invalid offsetof from non-POD type `struct foo_struct';
use pointer to member instead"

Here is the simplest test:

#include <stdio.h>
#include <linux/stddef.h>

class Myclass {
          int _val;
};

struct foo_struct {
          Myclass a;
          void *bar;
};

int main(void)
{
          printf("offset is %d\n", offsetof(struct foo_struct, bar));
}


The class within the struct is causing offsetof warning.  Could someone
describe why this is a warning and if this produces invalid code.  If this
is correect code, is there a way to turn off this warning?

TIA,
Chris




^ permalink raw reply	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2003-09-12  3:58 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-21 13:49 invalid offsetof from non-POD type John Quigley
2003-04-21 14:30 ` Gabriel Dos Reis
2003-04-22  2:40   ` John Quigley
2003-04-22  3:14     ` Gabriel Dos Reis
2003-04-22  4:49       ` Zack Weinberg
2003-04-22  6:32         ` Gabriel Dos Reis
2003-04-22 10:09           ` Kai Henningsen
2003-04-22 10:34             ` Gabriel Dos Reis
2003-04-22 10:50               ` Nathan Sidwell
2003-04-22 10:59                 ` Gabriel Dos Reis
2003-04-22 18:19                   ` Zack Weinberg
2003-04-22 19:35                     ` Joe Buck
2003-04-22 11:07                 ` Gabriel Dos Reis
2003-04-22 18:46                   ` Joe Buck
2003-04-22 18:37           ` Joe Buck
2003-04-22 19:24             ` Daniel Jacobowitz
2003-04-22 19:39               ` Joe Buck
2003-04-23  7:38               ` Gabriel Dos Reis
2003-04-23  7:42                 ` Gareth Pearce
2003-04-22  7:06       ` John Quigley
2003-04-22  8:51         ` Gabriel Dos Reis
2003-04-25  8:07           ` John Quigley
2003-04-25 12:14             ` Nathan Sidwell
2003-04-25 12:29               ` Gareth Pearce
2003-04-25 15:13               ` Paul Koning
2003-04-25 23:30             ` Matthias Benkmann
2003-04-26 14:42               ` Nathan Sidwell
2003-04-27 15:43                 ` Matthias Benkmann
2003-04-28 18:59                   ` Matt Austern
2003-04-21 14:50 ` Nathan Sidwell
  -- strict thread matches above, loose matches on Subject: below --
2003-09-12  4:32 Arnold Cole
2003-09-12  4:55 ` Gabriel Dos Reis
2003-08-12 13:38 skaller
2003-08-12 14:36 ` Fergus Henderson
2003-08-12 22:05   ` skaller
2003-08-13 20:59     ` Matt Austern
2003-08-14  8:20       ` skaller
2003-01-10  7:28 Chris Croswhite

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).