public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3
@ 1999-12-17 14:24 cris
  1999-12-17 15:54 ` Mike Albaugh
  1999-12-31 22:24 ` cris
  0 siblings, 2 replies; 4+ messages in thread
From: cris @ 1999-12-17 14:24 UTC (permalink / raw)
  To: help-gcc

hi!

how can i tell gcc to compile my programs using strict-alignment for data
types?
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!

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!

please help, reply by email too!

have a nice day :)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3
  1999-12-17 14:24 ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3 cris
@ 1999-12-17 15:54 ` Mike Albaugh
  1999-12-31 22:24   ` Mike Albaugh
  1999-12-31 22:24 ` cris
  1 sibling, 1 reply; 4+ messages in thread
From: Mike Albaugh @ 1999-12-17 15:54 UTC (permalink / raw)
  To: help-gcc

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" :-)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3
  1999-12-17 15:54 ` Mike Albaugh
@ 1999-12-31 22:24   ` Mike Albaugh
  0 siblings, 0 replies; 4+ messages in thread
From: Mike Albaugh @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

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" :-)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3
  1999-12-17 14:24 ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3 cris
  1999-12-17 15:54 ` Mike Albaugh
@ 1999-12-31 22:24 ` cris
  1 sibling, 0 replies; 4+ messages in thread
From: cris @ 1999-12-31 22:24 UTC (permalink / raw)
  To: help-gcc

hi!

how can i tell gcc to compile my programs using strict-alignment for data
types?
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!

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!

please help, reply by email too!

have a nice day :)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-12-31 22:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-12-17 14:24 ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3 cris
1999-12-17 15:54 ` Mike Albaugh
1999-12-31 22:24   ` Mike Albaugh
1999-12-31 22:24 ` cris

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).