Memory allocation and the OS/processor

Perhaps this is the wrong forum for this sort of question but since I am using C++ and need to write a memory manager, I thought I would ask it here. I am wondering though if this question is OS or processor dependent.

I know that the processor (or OS) can handle 8 bytes at a time in 64 bit OS's and 4 in 32. Does this impose restrictions on how memory is allocated? Or does efficiency impose restrictions? Will a 13 byte object take 13 bytes or will it take 16? How about a 9 byte object or a 1 byte object? I guess what I am asking is, does memory allocation occur based on actual need or is it segmented based on the width of data formats for the sake of efficiency and reducing potential collision errors (if that is even an issue)?
Last edited on
I need to write a memory manager

Well, that's far too vague to be of much use.

As is usual, the best way to get help with an issue is to explain what specific problem you are trying to solve. Else you risk exploring an XY problem.
http://xyproblem.info/
What specific constraints does your solution need to meet?

Does [the system architecture] impose restrictions on how memory is allocated?

Yes, the system's memory architecture can have observable effects even at the application level, but we'd prefer not to worry about it, except in certain optimizations or at maybe at a lower level than the system malloc.

If by "memory manager", you mean you want a pool allocator, then you won't generally need to bother with sourcing memory from anything other than new and delete (or the upstream allocator), and there are plenty high-quality implementations of this already.

Will a 13 byte object take 13 bytes or will it take 16?

A 13-byte object generally needs to be allocated on a 16-byte boundary. Violating the alignment requirement on C++ objects will yield undefined behavior (some processors are fine, others merely trap). You may obtain the required alignment with the alignof operator, align with alignas, or obtain an aligned pointer in raw storage with std::align().

Standard malloc and new (new char[n]) return maximally-aligned blocks of memory, so that the resulting raw storage always (barring over-aligned objects) accommodates the alignment requirements of the objects that occupy it.
Last edited on
thanks for the info @mbozzi. That you gave me enough info to answer my question.
Topic archived. No new replies allowed.