Namespaces
Variants
Views
Actions

Difference between revisions of "c/program/memalignment"

From cppreference.com
< c‎ | program
(N2974 memalignment)
 
m
Line 7: Line 7:
 
Returns the maximum alignment satisfied by the provided address. The return value can be greater than any alignment value supported by the implementation. If {{tt|p}} is a null pointer value, {{c|0}} is returned to indicate that the pointer cannot be used to access an object of any type.
 
Returns the maximum alignment satisfied by the provided address. The return value can be greater than any alignment value supported by the implementation. If {{tt|p}} is a null pointer value, {{c|0}} is returned to indicate that the pointer cannot be used to access an object of any type.
  
If the return value compares is greater than or equal to {{c|alignof(T)}}, the alignment requirement for the type {{tt|T}} is satisfied.
+
If the return value compares is greater than or equal to {{c|alignof(T)}}, the alignment requirement for the type {{tt|T}} is satisfied by the pointer.
  
 
A [[c/language/conformance|freestanding implementation]] needs to provide {{tt|memalignment}}.
 
A [[c/language/conformance|freestanding implementation]] needs to provide {{tt|memalignment}}.
Line 22: Line 22:
 
On common platforms where
 
On common platforms where
 
* null pointers are cast to integer {{c|0}},
 
* null pointers are cast to integer {{c|0}},
* pointer values are directly cast to virtual addresses, and
+
* pointer values are directly cast to the numeric values of virtual addresses, and
 
* {{lc|size_t}} is same as {{lc|uintptr_t}},
 
* {{lc|size_t}} is same as {{lc|uintptr_t}},
 
this function can be implemented as {{c|return (size_t)p & -(size_t)p;}}.
 
this function can be implemented as {{c|return (size_t)p & -(size_t)p;}}.

Revision as of 02:47, 10 October 2024

Defined in header <stdlib.h>
size_t memalignment( const void *p );
(since C23)

Returns the maximum alignment satisfied by the provided address. The return value can be greater than any alignment value supported by the implementation. If p is a null pointer value, 0 is returned to indicate that the pointer cannot be used to access an object of any type.

If the return value compares is greater than or equal to alignof(T), the alignment requirement for the type T is satisfied by the pointer.

A freestanding implementation needs to provide memalignment.

Contents

Parameters

p - pointer to query alignment

Return value

The alignment value of p, or 0 if p is a null pointer value.

Notes

On common platforms where

  • null pointers are cast to integer 0,
  • pointer values are directly cast to the numeric values of virtual addresses, and
  • size_t is same as uintptr_t,

this function can be implemented as return (size_t)p & -(size_t)p;.

Example

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    alignas(128) int i = 0;
    printf("%zu\n%zu\n", memalignment(nullptr), memalignment(&i));
}

Possible output:

0
128

References

  • C23 standard (ISO/IEC 9899:2024):
  • 7.24.9.1 The memalignment function (p: 374)