Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/align"

From cppreference.com
< cpp‎ | memory
m
m (Shorten template names. Use {{lc}} where appropriate.)
Line 11: Line 11:
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | alignment | the desired alignment }}
+
{{par | alignment | the desired alignment }}
{{param list item | size | the size of the storage to be aligned}}
+
{{par | size | the size of the storage to be aligned}}
{{param list item | ptr | pointer to contiguous storage of at least {{tt|space}} bytes}}
+
{{par | ptr | pointer to contiguous storage of at least {{tt|space}} bytes}}
{{param list item | space | the size of the buffer in which to operate}}
+
{{par | space | the size of the buffer in which to operate}}
{{param list end}}
+
{{par end}}
  
 
===Return value===
 
===Return value===
Line 77: Line 77:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/language/dcl list alignof}}
+
{{dsc inc | cpp/language/dcl list alignof}}
{{dcl list template | cpp/language/dcl list alignas}}
+
{{dsc inc | cpp/language/dcl list alignas}}
{{dcl list template | cpp/types/dcl list aligned_storage}}
+
{{dsc inc | cpp/types/dcl list aligned_storage}}
{{dcl list end}}
+
{{dsc end}}
  
 
[[de:cpp/memory/align]]
 
[[de:cpp/memory/align]]

Revision as of 19:12, 31 May 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
Defined in header <memory>
void* align( std::size_t alignment,

             std::size_t size,
             void*& ptr,

             std::size_t& space );
(since C++11)

If it is possible to fit size bytes of storage aligned by alignment into the buffer pointed to by ptr with length space, the function modifies ptr to point to the first possible address of such aligned storage and decreases space by the number of bytes used for alignment. If it is impossible (the buffer is too small), align does nothing.

Contents

Parameters

alignment - the desired alignment
size - the size of the storage to be aligned
ptr - pointer to contiguous storage of at least space bytes
space - the size of the buffer in which to operate

Return value

The adjusted value of ptr, or null pointer value if the space provided is too small.

Example

demonstrates the use of std::align to place objects of different type in memory

#include <iostream>
#include <memory>
 
template <std::size_t N>
struct MyAllocator
{
    char data[N];
    void* p;
    std::size_t sz;
    MyAllocator() : p(data), sz(N) {}
    template <typename T>
    T* aligned_alloc(std::size_t a = alignof(T))
    {
        if (std::align(a, sizeof(T), p, sz))
        {
            T* result = reinterpret_cast<T*>(p);
            p = (char*)p + sizeof(T);
            return result;
        }
        return nullptr;
    }
};
 
int main()
{
    MyAllocator<64> a;
 
    // allocate a char
    char* p1 = a.aligned_alloc<char>();
    if (p1)
        *p1 = 'a';
    std::cout << "allocated a char at " << (void*)p1 << '\n';
 
    // allocate an int
    int* p2 = a.aligned_alloc<int>();
    if (p2)
        *p2 = 1;
    std::cout << "allocated an int at " << (void*)p2 << '\n';
 
    // allocate an int, aligned at 32-byte boundary
    int* p3 = a.aligned_alloc<int>(32);
    if (p3)
        *p3 = 2;
    std::cout << "allocated an int at " << (void*)p3 << " (32 byte alignment)\n";
}

Possible output:

allocated a char at 0x2ff21a08
allocated an int at 0x2ff21a0c
allocated an int at 0x2ff21a20 (32 byte alignment)

See also

Template:cpp/language/dcl list alignofTemplate:cpp/language/dcl list alignas
(C++11)(deprecated in C++23)
defines the type suitable for use as uninitialized storage for types of given size
(class template) [edit]