0

Suppose that a cpu reads a word that truncates an integer. I've read that if structure padding is not enabled the CPU would have to do two reads: it has to read in the first half, then read in the second half separately, then reassemble them together to do the computation.

How does a cpu notices that an integer (for example) has been truncated ?

Koinos
  • 151
  • 3
  • 14

1 Answers1

1

This depends on the CPU, and on what instructions your compiler will generate. Some CPUs will happily perform unaligned loads (basically, they read the two halves and recombine them for you). Some will silently return corrupted data, and some will generate an exception and cause your program to crash immediately. Sometimes a CPU will have multiple instructions that can load and store data, some allow unaligned access, some don't.

The best way to find out what is happening on your CPU is to test it out. Or, look at the assembly generated by your compiler, and look up those assembly instructions in your CPU's manual to find out what it is going to do.

See this question for more information if you have an Intel or AMD CPU: What's the actual effect of successful unaligned accesses on x86?

G. Sliepen
  • 7,637
  • 1
  • 15
  • 31