[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Alignment



  If I cast a const char * such as "MiNT" or "LIVE" to a long *, shouldn't
  the compiler store the string so that it is aligned to a long boundary?
  Otherwise, when trying to read the long at that address, it could be on
  an odd address, and the machine dies.
  
  Lately, that is the way I've been reading strings to compare to cookie tags.
  It has always worked, but when I changed main.c in mint, it crashes and I'm
  pretty sure its because the 68K can't read a long from an odd address.  Is
  this a bug in the compiler, or is such a cast simply bad programming?  If
  this is a bug in 2.4.5 and it will work in 2.5.8, then lemme know and I can
  post the diffs for the cookie jar call and DOM_X (partially implemented).

How you use pointer does nothing about what it points to. Just because
you cast a (char *) to a (long *) doesn't tell the compiler that thus-
and-such symbol needs to be longword aligned.

Usually the compiler tries to start every data object at an even
boundary, so I'm surprised that you could have made a change that broke
things. The first thing to check whenever you suspect this sort of problem
is to recompile the file with the -S option and look at the assembler
code output. If the declaration of the string in question is preceded
by a directive like ".even" then this can't be the problem, and you'll
have to look somewhere else. If not, then inserting a ".even" in the code
and assembling the file should yield a running program. If that *is*
the solution, then it means you have to change something in the order
of declarations in your C source code.

All in all, yes, this is bad programming practice, because it means you've
become dependent on the particular behavior of your linker and other utilities,
and they offer no guarantees about ordering of data objects in memory...