public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: albaugh@agames.com (Mike Albaugh)
To: help-gcc@gnu.org
Subject: Re: ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3
Date: Fri, 17 Dec 1999 15:54:00 -0000	[thread overview]
Message-ID: <83ehv8$oc9$1@null.agames.com> (raw)
In-Reply-To: <83earg$bgg$1@nslave1.tin.it>

cris (chitty@libero.it) wrote:
: hi!

: how can i tell gcc to compile my programs using strict-alignment for data
: types?

	It seems rather that you are trying to do the opposite.

: if i write smth like this:

:     void *full_array = malloc(50);
:     short *short_p = &(( (char*) full_array)[ N ]);

:     printf("%hd\n",*short_p);

: if N is odd, i get a core dump!

	Well, yes. You _asked_ it to get a short from an odd address
malloc() always returns "maximum possible needed alignment" memory,
so the address will tend to have several zeros in the LSBs. Add
an od number to that and you get an odd number, so why would you
_not_ expect a problem on any machine with any alignment issues?

: i'm using gcc 2.7.2 on an old sparc2 with SunOs 4.1.3, no way to upgrade the
: os or to get a newer build of gcc.

: i've searched in the doc., i've found -m switches regarding alignment for
: ibm machines, but none for sparc ones!

: i've also seen a SHORT_ALIGNMENT macro to be defined (gcc-info, "You can
: control compilation driver"), but i don't know WHERE to define it!

	Simply put, there is no way to get an "already compiled" library
routine to "know" that it has to pick up every larger-than-one-byte
data item "with tweezers". Even if there was, would you really want to
use such a system? Think about it, _every_ memory reference done a
byte at a time unless the compiler can "prove" that it can't fault.
This is the "nasty little secret" about __attribute__ aligned (or
prgama pack). The compiler "knows" to "be careful" in the piece
of code that actually contains the structure definition, but if
you take the address of mis-aligned short within a "packed" structure,
and pass it to a function that expects a legitimate "pointer to short",
"bad things could happen" (Don't cross the beams!)

					Mike
| albaugh@agames.com, speaking only for myself, and soon to abandon Usenet,
| at least for a while, Happy "end of civilization as we know it" :-)

WARNING: multiple messages have this Message-ID
From: albaugh@agames.com (Mike Albaugh)
To: help-gcc@gnu.org
Subject: Re: ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3
Date: Fri, 31 Dec 1999 22:24:00 -0000	[thread overview]
Message-ID: <83ehv8$oc9$1@null.agames.com> (raw)
Message-ID: <19991231222400.TQSSTYdm8TmFEJsx08PP_6A1rOVN_M07LYVD9dGdJGg@z> (raw)
In-Reply-To: <83earg$bgg$1@nslave1.tin.it>

cris (chitty@libero.it) wrote:
: hi!

: how can i tell gcc to compile my programs using strict-alignment for data
: types?

	It seems rather that you are trying to do the opposite.

: if i write smth like this:

:     void *full_array = malloc(50);
:     short *short_p = &(( (char*) full_array)[ N ]);

:     printf("%hd\n",*short_p);

: if N is odd, i get a core dump!

	Well, yes. You _asked_ it to get a short from an odd address
malloc() always returns "maximum possible needed alignment" memory,
so the address will tend to have several zeros in the LSBs. Add
an od number to that and you get an odd number, so why would you
_not_ expect a problem on any machine with any alignment issues?

: i'm using gcc 2.7.2 on an old sparc2 with SunOs 4.1.3, no way to upgrade the
: os or to get a newer build of gcc.

: i've searched in the doc., i've found -m switches regarding alignment for
: ibm machines, but none for sparc ones!

: i've also seen a SHORT_ALIGNMENT macro to be defined (gcc-info, "You can
: control compilation driver"), but i don't know WHERE to define it!

	Simply put, there is no way to get an "already compiled" library
routine to "know" that it has to pick up every larger-than-one-byte
data item "with tweezers". Even if there was, would you really want to
use such a system? Think about it, _every_ memory reference done a
byte at a time unless the compiler can "prove" that it can't fault.
This is the "nasty little secret" about __attribute__ aligned (or
prgama pack). The compiler "knows" to "be careful" in the piece
of code that actually contains the structure definition, but if
you take the address of mis-aligned short within a "packed" structure,
and pass it to a function that expects a legitimate "pointer to short",
"bad things could happen" (Don't cross the beams!)

					Mike
| albaugh@agames.com, speaking only for myself, and soon to abandon Usenet,
| at least for a while, Happy "end of civilization as we know it" :-)

  reply	other threads:[~1999-12-17 15:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-17 14:24 cris
1999-12-17 15:54 ` Mike Albaugh [this message]
1999-12-31 22:24   ` Mike Albaugh
1999-12-31 22:24 ` cris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='83ehv8$oc9$1@null.agames.com' \
    --to=albaugh@agames.com \
    --cc=help-gcc@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).