Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Alignment problems |
| Date | 2011-12-01 14:15 -0800 |
| Organization | None to speak of |
| Message-ID | <lnehwoos58.fsf@nuthaus.mib.org> (permalink) |
| References | <jb6gbf$58a$1@speranza.aioe.org> <jb80hb$ct8$1@dont-email.me> |
"BartC" <bc@freeuk.com> writes:
[...]
> Alternatively, don't compilers have a 'pack' directive for exactly this
> purpose? Again this would be conditional. Ideally a way of controlling
> alignment on a field-by-field basis is needed.
Yes, some compilers have a pack directive, but it doesn't always work.
For example, on SPARC, attempting to access a misaligned object causes
the program to crash. Here's a program that uses gcc's
__attribute__((packed)) extension:
#include <stdio.h>
int main(void) {
struct unpacked {
char c;
double d;
};
struct packed {
char c;
double d;
} __attribute__((packed));
struct unpacked u[2] = { { 'u', 1.2 }, { 'U', 3.4 } };
struct packed p[2] = { { 'p', 5.6 }, { 'P', 7.8 } };
double *up0 = &u[0].d;
double *up1 = &u[1].d;
double *pp0 = &p[0].d;
double *pp1 = &p[1].d;
printf("*up0 = %g\n", *up0);
printf("*up1 = %g\n", *up1);
printf("*pp0 = %g\n", *pp0);
printf("*pp1 = %g\n", *pp1);
return 0;
}
And here's the output I got:
*up0 = 1.2
*up1 = 3.4
Bus error
If you refer to the misaligned member directly, so the compiler
knows that it's misaligned, it can access it correctly. If you've
saved a pointer to the misaligned member, there's no way for the
compiler to know that it's misaligned.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Alignment problems jacob navia <jacob@spamsink.net> - 2011-12-01 01:05 +0100
Re: Alignment problems Ian Collins <ian-news@hotmail.com> - 2011-12-01 13:15 +1300
Re: Alignment problems pacman@kosh.dhis.org (Alan Curry) - 2011-12-01 02:57 +0000
Re: Alignment problems gordonb.hpln0@burditt.org (Gordon Burditt) - 2011-11-30 21:27 -0600
Re: Alignment problems jacob navia <jacob@spamsink.net> - 2011-12-01 13:38 +0100
Re: Alignment problems pacman@kosh.dhis.org (Alan Curry) - 2011-12-01 20:38 +0000
Re: Alignment problems jacob navia <jacob@spamsink.net> - 2011-12-01 21:50 +0100
Re: Alignment problems Ian Collins <ian-news@hotmail.com> - 2011-12-02 10:27 +1300
Re: Alignment problems Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-04 00:23 +0000
Re: Alignment problems Ian Collins <ian-news@hotmail.com> - 2011-12-04 13:31 +1300
Re: Alignment problems ImpalerCore <jadill33@gmail.com> - 2011-12-01 14:34 -0800
Re: Alignment problems "BartC" <bc@freeuk.com> - 2011-12-01 13:46 +0000
Re: Alignment problems Keith Thompson <kst-u@mib.org> - 2011-12-01 14:15 -0800
Re: Alignment problems David Thompson <dave.thompson2@verizon.net> - 2012-01-19 02:44 -0500
Re: Alignment problems "James" <no@spam.invalid> - 2011-12-01 19:43 -0800
Re: Alignment problems "Brain" <Brian@myhome.com> - 2011-12-04 02:47 -0600
Re: Alignment problems Keith Thompson <kst-u@mib.org> - 2011-12-04 08:26 -0800
Re: Alignment problems Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-08 00:12 +0200
Re: Alignment problems Keith Thompson <kst-u@mib.org> - 2011-12-07 22:31 -0800
Re: Alignment problems lovecreatesbeauty <lovecreatesbeauty@gmail.com> - 2011-12-08 21:43 -0800
Re: Alignment problems Keith Thompson <kst-u@mib.org> - 2011-12-09 01:31 -0800
Re: Alignment problems Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-12-09 14:15 +0200
csiph-web