align() in C++

align(): Fit aligned storage in the buffer. [ptr.align].This function tries to fit __size 

bytes of storage with alignment __align into the buffer __ptr of size __space bytes. 

If such a buffer fits then __ptr is changed to point to the first byte of the 

aligned storage and __space are reduced by the bytes used for alignment. 

This function returns the updated pointer if the aligned storage fits, otherwise nullptr.

Parameters: Four parameters are required for this function.

__align – A fundamental or extended alignment value. 

__size – Size of the aligned storage required.

 __ptr – Pointer to a buffer of __space bytes. 

__space – Size of the buffer pointed to by __ptr.

This is available in the below file.

File: memory

Syntax:

align(__align, __size, __ptr, __space)

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()

{
    char arr[] = "abcdef";

    void *pt = arr;
    size_t space = sizeof(arr) - 1;

    while (align(alignof(int), sizeof(char), ptspace))
    {

        //cast the void pointer into
        // char
        char *temp = static_cast<char *>(pt);
        ++temp;
        space -= sizeof(char);
        pt = temp;
    }

    cout << arr << "\n";
    return 0;
}


No comments:

Post a Comment