다음을 통해 공유


맞춤

지정된 크기의 저장소(지정된 정렬 사양을 기준으로 정렬됨)를 지정된 저장소의 첫 번째 가능한 주소에 정렬합니다.

void* align(
    size_t Alignment, // input
    size_t Size,      // input
    void*& Ptr        // input/output
    size_t& Space     // input/output
);

매개 변수

  • Alignment
    시도에 맞게 정렬됩니다.

  • Size
    정렬된 저장소의 크기(바이트 단위)입니다.

  • Ptr
    사용 가능한 인접 저장소 풀 중 사용할 저장소 풀의 시작 주소입니다. 이 매개 변수는 출력 매개 변수이기도 하며 정렬이 성공할 경우 새 시작 주소가 포함됩니다.

    **align()**이 성공하지 않을 경우 이 매개 변수는 수정되지 않습니다.

  • Space
    정렬된 저장소를 만드는 데 사용하기 위해 **align()**에서 사용할 수 있는 총 공간입니다. 이 매개 변수는 출력 매개 변수이기도 하며, 정렬된 저장소 및 관련된 모든 연결된 오버헤드를 차감한 후 저장소 버퍼에 남아 있는 조정된 공간을 포함합니다.

    **align()**이 성공하지 않을 경우 이 매개 변수는 수정되지 않습니다.

반환 값

요청된 정렬된 버퍼가 사용 가능 공간에 맞지 않는 경우 null 포인터이며, 그렇지 않을 경우 Ptr의 새 값입니다.

설명

수정된 Ptr 및 Space 매개 변수를 사용하면 Alignment 및 Size에 대해 다른 값을 사용하여 동일한 버퍼에서 **align()**을 반복적으로 호출할 수 있습니다. 다음 코드 조각은 **align()**을 사용하는 방법을 보여 줍니다.

#include <type_traits> // std::alignment_of()
#include <memory>
//...
char buffer[256]; // for simplicity
size_t alignment = std::alignment_of<int>::value;
void * ptr = buffer;
std::size_t space = sizeof(buffer); // Be sure this results in the true size of your buffer

while (alignment, sizeof(MyObj), ptr, space)) {
    // You now have storage the size of MyObj, starting at ptr, aligned on 
    // int boundary. Use it here if you like, or save off the starting address
    // contained in ptr for later use.
    // ...
    // Last, move starting pointer and decrease available space before
    // the while loop restarts.
    ptr = reinterpret_cast<char*>(ptr) + sizeof(MyObj);
    space -= sizeof(MyObj);
}
// At this point, align() has returned a null pointer, signaling it is not
// possible to allow more aligned storage in this buffer.

요구 사항

헤더: <memory>

네임스페이스: std

참고 항목

참조

<memory>