* Question about unaligned pointer
@ 2022-01-18 1:54 Bin.Cheng
2022-01-18 7:20 ` Alexander Monakov
0 siblings, 1 reply; 4+ messages in thread
From: Bin.Cheng @ 2022-01-18 1:54 UTC (permalink / raw)
To: gcc-help
Hi,
With -Waddress-of-packed-member option, GCC gives warning message if
address of packed structure member is taken and assigned to pointer,
like:
struct foo {
char a;
int b;
} __attribute__((packed));
int main()
{
struct foo foo;
int *p;
p = &foo.b; // !!
*p = 1234;
return 0;
}
This is expected, however, I wonder if there is any way to let gcc
know that `p` is a pointer that might be unaligned, so that an
unaligned access instruction is generated if `p` is dereferenced?
IIRC, microsoft compiler has __unaligned keyword extended for pointer
declaration.
Thanks,
bin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Question about unaligned pointer
2022-01-18 1:54 Question about unaligned pointer Bin.Cheng
@ 2022-01-18 7:20 ` Alexander Monakov
2022-01-19 1:46 ` Bin.Cheng
0 siblings, 1 reply; 4+ messages in thread
From: Alexander Monakov @ 2022-01-18 7:20 UTC (permalink / raw)
To: Bin.Cheng; +Cc: gcc-help
On Tue, 18 Jan 2022, Bin.Cheng via Gcc-help wrote:
> Hi,
> With -Waddress-of-packed-member option, GCC gives warning message if
> address of packed structure member is taken and assigned to pointer,
> like:
> struct foo {
> char a;
> int b;
> } __attribute__((packed));
>
> int main()
> {
> struct foo foo;
> int *p;
> p = &foo.b; // !!
> *p = 1234;
> return 0;
> }
>
> This is expected, however, I wonder if there is any way to let gcc
> know that `p` is a pointer that might be unaligned, so that an
> unaligned access instruction is generated if `p` is dereferenced?
> IIRC, microsoft compiler has __unaligned keyword extended for pointer
> declaration.
Sure, just add a new type with non-standard alignment. Instead of
'int *p' you can write e.g.
typedef int i32u __attribute__((aligned(1)));
i32u *p;
Alexander
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Question about unaligned pointer
2022-01-18 7:20 ` Alexander Monakov
@ 2022-01-19 1:46 ` Bin.Cheng
2022-01-19 16:41 ` Alexander Monakov
0 siblings, 1 reply; 4+ messages in thread
From: Bin.Cheng @ 2022-01-19 1:46 UTC (permalink / raw)
To: Alexander Monakov; +Cc: gcc-help
On Tue, Jan 18, 2022 at 3:20 PM Alexander Monakov <amonakov@ispras.ru> wrote:
>
> On Tue, 18 Jan 2022, Bin.Cheng via Gcc-help wrote:
>
> > Hi,
> > With -Waddress-of-packed-member option, GCC gives warning message if
> > address of packed structure member is taken and assigned to pointer,
> > like:
> > struct foo {
> > char a;
> > int b;
> > } __attribute__((packed));
> >
> > int main()
> > {
> > struct foo foo;
> > int *p;
> > p = &foo.b; // !!
> > *p = 1234;
> > return 0;
> > }
> >
> > This is expected, however, I wonder if there is any way to let gcc
> > know that `p` is a pointer that might be unaligned, so that an
> > unaligned access instruction is generated if `p` is dereferenced?
> > IIRC, microsoft compiler has __unaligned keyword extended for pointer
> > declaration.
>
> Sure, just add a new type with non-standard alignment. Instead of
> 'int *p' you can write e.g.
>
> typedef int i32u __attribute__((aligned(1)));
> i32u *p;
>
Hi Alexander,
Thanks very much for helping. This works.
However, I wonder what the difference is between typedef and the following code?
int __attribute__((aligned(1))) *p;
// ...
Thanks,
bin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Question about unaligned pointer
2022-01-19 1:46 ` Bin.Cheng
@ 2022-01-19 16:41 ` Alexander Monakov
0 siblings, 0 replies; 4+ messages in thread
From: Alexander Monakov @ 2022-01-19 16:41 UTC (permalink / raw)
To: Bin.Cheng; +Cc: gcc-help
On Wed, 19 Jan 2022, Bin.Cheng wrote:
> > Sure, just add a new type with non-standard alignment. Instead of
> > 'int *p' you can write e.g.
> >
> > typedef int i32u __attribute__((aligned(1)));
> > i32u *p;
> >
> Hi Alexander,
> Thanks very much for helping. This works.
> However, I wonder what the difference is between typedef and the following code?
>
> int __attribute__((aligned(1))) *p;
Here the attribute applies to the thing being declared, i.e. the variable p, not
the type it points to. You can verify this by e.g. printing __alignof(p) and
__alignof(*p).
Alexander
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-01-19 16:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-18 1:54 Question about unaligned pointer Bin.Cheng
2022-01-18 7:20 ` Alexander Monakov
2022-01-19 1:46 ` Bin.Cheng
2022-01-19 16:41 ` Alexander Monakov
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).