2

I have some binary data in a file and load the file into memory at char* data. Now I know e.g. that at offset 123 begins a struct something.

Is it safe to do the following

(struct something*) (data + 123) // ??&data [123]??

and then access members of the struct. Or should I do something like

struct something record;
memcpy (&record, data + 123, sizeof (struct something) );

Or even something completely different?

My question is mainly motivated, because I have some fuzzy recollections about "memory alignment", "word boundaries", etc...

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87

2 Answers2

6

Yes, you should use memcpy, not pointer conversion, for exactly the reason you discuss: alignment. Converting pointers is not safe. If the alignment is wrong, the behavior when you use the resulting pointer is not guaranteed by the C standard.

An alternative in some situations is to read the data directly into the object.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Thank you very much. I feared so and I will stick to `memcpy`. On a side note: Is there any difference between `data + 123`, `&data[123]` and `&123[data]`? Or just syntactical sugar? – Hyperboreus Jan 28 '14 at 01:49
  • @Hyperboreus: For your purposes, there are no differences between those expressions. (I am tempted to say there are none, except you might need parentheses to avoid precedence issues; e.g. `pointer - data + 123` is different from `pointer - &data[123]`, and but `pointer - (data + 123)` would be the same as the latter. However, to ensure there are no differences even in esoteric situations, I would have to check the C standard, and I am pressed for time at the moment.) – Eric Postpischil Jan 28 '14 at 02:58
  • One side note: on arm built-in `memcpy` frequently causes dumps when used with misaligned data, so you might need to disable built-in `memcpy` and `memmove` (and use libc versions) if you encounter that problem (its compiler-dependent). – Valeri Atamaniouk Jan 28 '14 at 08:18
0

It depends.

On some modern CPUs(like x86), it's OK to access unaligned word for read/write operation, and there is no performance penalty, though some specific instructions might require aligned operand (like the CAS instruction on PPC).

tristan
  • 4,235
  • 2
  • 21
  • 45