This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/56394] pointer arithmetic breaks with __attribute__((packed))
- From: "maxim.yegorushkin at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 19 Feb 2013 14:20:44 +0000
- Subject: [Bug c++/56394] pointer arithmetic breaks with __attribute__((packed))
- Auto-submitted: auto-generated
- References: <bug-56394-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56394
Maxim Yegorushkin <maxim.yegorushkin at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |UNCONFIRMED
Resolution|INVALID |
--- Comment #3 from Maxim Yegorushkin <maxim.yegorushkin at gmail dot com> 2013-02-19 14:20:44 UTC ---
I can reproduce this without __attribute__((packed)) actually:
$ cat test.cc
#include <cstdio>
#include <cstdint>
struct Xyz
{
uint32_t seq_no;
uint32_t end_offset_lo, end_offset_hi;
};
struct Mapping
{
void* mem;
size_t len;
template<class T>
T* begin() {
return static_cast<T*>(mem);
}
template<class T>
T* end() {
auto e = reinterpret_cast<uintptr_t>(static_cast<char*>(mem) +
len);
return reinterpret_cast<T*>(e - e % sizeof(T));
}
};
int main() {
Mapping m{reinterpret_cast<void*>(0x40000u), 0x1000u};
Xyz* beg = m.begin<Xyz>();
Xyz* end = m.end<Xyz>();
size_t len = end - beg;
printf("beg = %p\n", beg);
printf("end = %p\n", end);
printf("end - beg = %zx\n", len);
printf("sizeof(Xyz) == %zu\n", sizeof(Xyz));
}
$ g++ -o test -std=gnu++11 -Wall -Wextra test.cc
$ ./test
beg = 0x40000
end = 0x40ff8
end - beg = 55555555555556aa <---- wrong
sizeof(Xyz) == 12
$ g++ -o test -std=gnu++11 -Wall -Wextra -O test.cc
$ ./test
beg = 0x40000
end = 0x40ff8
end - beg = 154 <---- correct
sizeof(Xyz) == 12