This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Do all arrays have to have a pad byte after them?


Dave Brolley writes:
 > While comparing the pointers one past the end is defined, dereferencing
 > them is not. There is no need for a padding byte to actually be allocated
 > in memory. Even so, I would expect the test to print "failed" when
 > compiled with most compilers.

I repeat the quote from the standard, section 5.10 -

   Two pointers of the same type compare equal if and only if they are
   both null, both point to the same object or function, or both point
   one past the end of the same array.

And here's my code:

	extern "C" int printf(const char *, ...);
	int main()
	{
		struct { char a[8], b[8]; } z;
		bool b = &z.a[8] == &z.b[0];
		printf("%s\n", b ? "failed" : "succeeded");
	}

Now, &z.a[8] points one past the end of array z.a, and &z.b[0] points
at z.b[0]. If these two pointers compare equal, then the compiler is
in violation of the "only if" part of the quoted section. I know most
compilers will fail this, including egcs. That's why I reported this
as a bug. Is my analysis incorrect?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]