0

I need to pass a font that is loaded to the memory from a file with fstream into a FT library function. It needs to be the base char pointer. Now after some reading I am in doubt whether I need to allocate actually aligned memory storage to load the file. I think when a font file is created, it has data of different types in it, and if the file is loaded in an unaligned char array (that is, it begins at the unaligned base address) there can be some issues (at a minimum, performance issues) when reading the data. Or maybe I'm wrong and everything's fine.

Oleksa
  • 635
  • 5
  • 15
  • 1
    probably not... – Mansoor Aug 20 '20 at 13:28
  • 3
    That depends on the *FT library function*. It could expect a data aligned in a certain way, or it could be aligment-agnostic. Alignment requirements should be specified as an API interface, and normally, absent any specific requirement, default alignment for input type is expected, which in case of `char*` is one. Last, but not the least, if you are dealing with x86, unaligned access has became largely non-issue. – SergeyA Aug 20 '20 at 13:49
  • @SergeyA, it is already seems to be inefficient when I pass int's, double's, etc that is not aligned properly. How the function can go around this and read something in efficient manner? – Oleksa Aug 20 '20 at 13:53
  • @olekstolar *it is already seems to be inefficient when I pass int's, double's, etc that is not aligned properly* - citation needed. – SergeyA Aug 20 '20 at 13:54
  • About what? About inefficiency of accessing unaligned data? – Oleksa Aug 20 '20 at 13:56
  • 1
    Yes, exactly. Inefficiency of accessing unaligned data on modern x86 CPUs. – SergeyA Aug 20 '20 at 13:57
  • If it were not an issue, then there wouldn't be in-structure padding, for example, but compilers do this. Also there is some articles like [Data alignment: Straighten up and fly right](https://developer.ibm.com/technologies/systems/articles/pa-dalign/) and answers like [this](https://stackoverflow.com/a/381368/9486175) that raises some doubts whether I should pass unaligned storage or not. – Oleksa Aug 20 '20 at 14:25

1 Answers1

0

After further investigation I've found that I don't need to do aligning explicitly. From the standard about allocation functions [basic.stc.dynamic.allocation]:

The pointer returned shall be suitably aligned so that it can be converted to a pointer to any suitable complete object type..

From cppreference.com about std::max_align_t:

Pointers returned by allocation functions such as std::malloc are suitably aligned for any object, which means they are aligned at least as strictly as std::max_align_t.

Therefore aligning is already handled by the compiler.

Oleksa
  • 635
  • 5
  • 15