Jump to content
  • Advertisement
mychii

C++ Memory management related questions

Recommended Posts

Hi,

I have a few basic questions about more memory management related to data alignment. I'm focusing on 64-bit architecture by the way, if that matters.

  1. Say I reserve a big pool for my app. How do I properly set the size? I heard it's 1024 with the power of 2 or 3, then multiply from that? If true, why. If not, why.
  2. How do I properly align data in 64-bit architecture? Is it by size? For example, size of int8 on any address, size of int64 on 8-byte aligned base address, and size of more than that on 16-byte aligned address?
  3. Does malloc return an aligned address? If yes by how many bytes (or depends on architecture)?  Or I shouldn't assume at all and always have to align it first manually?

If there's anything else that may be related to these I should know, please let me know.

Thanks.

Share this post


Link to post
Share on other sites
Advertisement
  1. I'm not sure what you mean here, besides malloc(size)... Can you rephrase what you mean? You heard that what is 1024?
  2. On x86-64 (AMD/Intel) yes it's generally the same as the primitive size. However, instead of using sizeof(T) as a guess, you can now just use alignof(T) and can be certain that it's correct and cross-platform to other architectures too!
  3. Yes, malloc is specified to use an alignment that's correct for any primitive type. Generally this means that it aligns to 8 bytes, which may be excessive for some uses (e.g. if you're ever allocating individual bytes, don't use malloc!) and may be too small for some other types (e.g. if you're using __m128 SSE intrinsics, don't use malloc!).

Share this post


Link to post
Share on other sites
21 minutes ago, Hodgman said:
  1. I'm not sure what you mean here, besides malloc(size)... Can you rephrase what you mean? You heard that what is 1024?
  2. On x86-64 (AMD/Intel) yes it's generally the same as the primitive size. However, instead of using sizeof(T) as a guess, you can now just use alignof(T) and can be certain that it's correct and cross-platform to other architectures too!
  3. Yes, malloc is specified to use an alignment that's correct for any primitive type. Generally this means that it aligns to 8 bytes, which may be excessive for some uses (e.g. if you're ever allocating individual bytes, don't use malloc!) and may be too small for some other types (e.g. if you're using __m128 SSE intrinsics, don't use malloc!).

Thanks for the answers of the other two, definitely forgot about alignof. I'll look into that deeper. x)

As for the first one, sorry I was not being clear. What I mean by reserving a big pool is I perform a big sized malloc (malloc(size), yes) for the whole app. As I was searching for this in Google (what size should I properly set), I stumbled on an opinion saying it may be good to set the size with a formula of 1024 with the power of 2 (or 3) then multiply it from there. For example, 1,024 (1 KB), 2,048 (2 KB), 4,096 (4 KB), etc., or 1,048,576 (1 MB), 2097152 (2 MB), etc., or 1,073,741,824 (1 GB), 2,147,483,648 (2 GB), etc., instead of straight 1 or 2 million MB or GB memory request. I'm sorry I couldn't find the discussion anymore so I couldn't link it here. I can only assume it relates to alignment, like maybe not wasting space near the boundaries if the memory use is well-aligned (for example, malloc(1000) for an array of 32 bytes data wastes 8 bytes, unlike using malloc(1024)), but not sure if it really is or there's more. What do you think? Does it matter?

Share this post


Link to post
Share on other sites
4 hours ago, mychii said:

Hi,

I have a few basic questions about more memory management related to data alignment. I'm focusing on 64-bit architecture by the way, if that matters.

  1. Say I reserve a big pool for my app. How do I properly set the size? I heard it's 1024 with the power of 2 or 3, then multiply from that? If true, why. If not, why.
  2. How do I properly align data in 64-bit architecture? Is it by size? For example, size of int8 on any address, size of int64 on 8-byte aligned base address, and size of more than that on 16-byte aligned address?
  3. Does malloc return an aligned address? If yes by how many bytes (or depends on architecture)?  Or I shouldn't assume at all and always have to align it first manually?

If there's anything else that may be related to these I should know, please let me know.

Thanks.

Typically malloc returns memory aligned to (at least) the most restrictive type on a given architecture.  You really don't have to worry about it unless you are doing something special. For instance it might return 4 byte aligned memory on a 32 bit machine and 8 byte aligned memory on a 64 bit machine. Since malloc has no idea what you are going to do with the memory, it has to do this.  If I remember correctly intel CPUs don't specifically require alignment but there used to be a performance penalty if your data wasn't aligned correctly. If you did this on some RISC processors however you would get the dreaded "bus error" and your program would crash, so in general it's best to make sure everything is aligned, but again unless you are doing something odd, it shouldn't be a problem.

If you are doing a custom allocator, like a slab allocator or some such, then you have to worry about such things.  Generally  however, you again just align to the natural architecture alignment. As for pool sizes, that's going to vary from machine to machine, depending on page sizes and probably other things. 

One trick I use on Windows 64 is to use. VirtualAlloc.  This lets you reserve a giant chunk of address space which is contiguous, yet not actually allocate the memory until needed.  You can get away with this because the address space is so huge, even if your memory isn't.

In any case any time you are allocating a lot of small objects you should generality do your own memory pool stuff for performance reasons assuming you care about that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!