Thread: dynamic memory alignment (Win32)

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    41

    dynamic memory alignment (Win32)

    should data stored in virtual address space (using VirtualAlloc()) be aligned? If so how should I store structures in virtual address space so that they are properly aligned?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The range you try to allocate must be properly aligned to page boundaries, but the data you put there must not be aligned.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    41
    that's because the OS takes care to align data accessed by the CPU whenever it writes/reads variables in dynamic memory?

    And if I use VirtualLock() to bring a region of address space in physical memory, do I have to worry about having data aligned?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by ZeroMemory View Post
    that's because the OS takes care to align data accessed by the CPU whenever it writes/reads variables in dynamic memory?
    The compiler will take care of aligning things. The CPU can read/write unaligned data too (on x86), but may take a performance hit.
    But if you are using VirtualAlloc, then it is best to align on a 4-byte boundary since you're basically working with pages the compiler won't be able to help.

    And if I use VirtualLock() to bring a region of address space in physical memory, do I have to worry about having data aligned?
    VirtualAlloc also works on pages. You only have to worry about the correct page boundary to make it work. The rest of the data should preferably be aligned, but even if not, it will work, but not always optimally.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  2. memory alignment of a struct
    By George2 in forum C Programming
    Replies: 4
    Last Post: 04-04-2007, 12:34 PM
  3. Issues with free and dynamic memory
    By dan s in forum C Programming
    Replies: 3
    Last Post: 01-14-2007, 03:44 AM
  4. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM