New: wrong alignment with pragma pack(4)
6 answers - 700 bytes -

Simple testcase:
#include <stdio.h>
#pragma pack(4)
struct ABC
{
double a;
int b;
bool c;
};
int main()
{
ABC A;
ABC B;
int C;
ABC D;
printf("A - %p; B - %p; C - %p; D - %p\n", &A, &B, &C, &D);
return 0;
}
compiled with:
gcc test.cpp -lstdc++
Variable D is aligned on 4 bytes and should be 8.
gcc -v:
Using built-in specs.
Target: sparc-sun-solaris2.8
Configured with: /gcc-4.1.1/configure /export/home/builder/gcc-4.1.1
,c++
Thread model: posix
gcc version 4.1.1
Problem originally observed on linux - debian 3.1 with x86 and target
sparc-sun-solaris2.8, exists also on original sparc host.
No.1 | | 257 bytes |
| 
Comment #1 from pinskia at gcc dot gnu dot org 2006-09-14 08:05
#pragma pack(4)
"Variable D is aligned on 4 bytes and should be 8."
No you said to align it to 4 bytes by that pack statement.
This is not a bug.
Why do you think otherwise?
No.2 | | 893 bytes |
| 
Comment #2 from dsiata at poczta dot onet dot pl 2006-09-14 08:14
Subject: Re: wrong alignment with pragma pack(4)
Uytkownik pinskia at gcc dot gnu dot org <gcc-bugzilla (AT) gcc (DOT) gnu.orgnapisa:
Comment #1 from pinskia at gcc dot gnu dot org 2006-09-14 08:05
>#pragma pack(4)
>"Variable D is aligned on 4 bytes and should be 8."
>No you said to align it to 4 bytes by that pack statement.
>
>This is not a bug.
>Why do you think otherwise?
>
>
Because double have to be aligned on 8 on sparc or bus error occurs.
Note that first member of the structure is double. So now I'm not able to
assign any value to D.a.
If pragma pack regards to all structures then should be some error reported if
structure inside pragma pack(4) contain double or long long
Regards
Dominik
No.3 | | 514 bytes |
| 
Comment #3 from pinskia at gcc dot gnu dot org 2006-09-14 08:19
(In reply to comment #2)
Because double have to be aligned on 8 on sparc or bus error occurs.
Note that first member of the structure is double. So now I'm not able to
assign any value to D.a.
Wrong, wrong, wrong. The compiler knows the alignment is not 8byte aligned so
it knows how to generate the code correctly. If that fails then you have a bug
with that but the code you gave is currently correct with what GCC gives.
No.4 | | 285 bytes |
| 
Comment #4 from dsiata at poczta dot onet dot pl 2006-09-14 08:57
(In reply to comment #3)
K. Now I see it's new feature in 4.0 or 4.1 series.
I have GPF in similar code but for now I cannot reproduce it on simple case
Thank you. Now I know to not use pack(4) :-)
No.5 | | 470 bytes |
| 
Comment #5 from dsiata at poczta dot onet dot pl 2006-09-14 10:46
(In reply to comment #3)
And what with such example:
#include <stdio.h>
#pragma pack(4)
struct ABC
{
double a;
int b;
bool c;
};
void Init(double& a)
{ a = 17.0; }
int main()
{
ABC A;
ABC B;
int C;
ABC D;
printf("D.a = %f\n", D.a);
Init(D.a);
printf("D.a = %f\n", D.a);
return 0;
}
There is Bus Error in Init function
No.6 | | 510 bytes |
| 
Comment #6 from pinskia at gcc dot gnu dot org 2006-09-14 14:56
(In reply to comment #5)
(In reply to comment #3)
And what with such example:
#include <stdio.h>
#pragma pack(4)
struct ABC
{
double a;
int b;
bool c;
};
void Init(double& a)
{ a = 17.0; }
int main()
{
ABC A;
ABC B;
int C;
ABC D;
printf("D.a = %f\n", D.a);
Init(D.a);
Nope, this testcase is invalid as you are violating the alignment requirement
for double pointers.