
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Alignof operator in C++
C++ alignof() Operator
The alignof operator is the operator that returns the memory alignment for a given variable type. This alignment tells how the data is arranged and accessed in memory. It returns the value in bytes and is the part of the <type_traits> header file in C++. which is mainly used for low-level programming like memory management or hardware requirements.
Syntax
Here is the following syntax of alignof operator, which returns memory alignment required for a given data type in bytes:
alignof(data_type)
Example Demonstrating Usage of alignof() Operator
Here is the following example code of using alignof() operator for built-in data types in C++, which is returning memory alignment in bytes.
#include <iostream> using namespace std; int main(){ cout<<"Memory alignment of char: "<<alignof(char)<< endl; cout<<"Memory alignment of int: "<<alignof(int)<<endl; cout<<"Memory alignment of float: "<<alignof(float)<< endl; cout<<"Memory alignment of double: "<<alignof(double)<< endl; cout<<"Memory alignment of pointer: "<<alignof(int*)<< endl; return 0; }
Output
Memory alignment of char: 1 Memory alignment of int: 4 Memory alignment of float: 4 Memory alignment of double: 8 Memory alignment of pointer: 8
Using alignof() Operator with Arrays and User-Defined Structures
In the following example, we are checking the memory alignment of user-defined data types like structures and arrays in C++.
#include <iostream> using namespace std; // a user-defined structure with members of different types struct basic { int i; float f; char s; }; // an empty structure struct Empty { }; int main(){ cout<<"Alignment of character array of 10 elements: "<<alignof(char[10])<<endl; cout<<"Alignment of integer array of 10 elements: "<<alignof(int[10])<<endl; cout<<"Alignment of float array of 10 elements: "<<alignof(float[10])<<endl; cout<<"Alignment of class basic: "<<alignof(basic)<<endl; cout<<"Alignment of Empty class: "<<alignof(Empty)<<endl; return 0; }
Output
Alignment of character array of 10 elements: 1 Alignment of integer array of 10 elements: 4 Alignment of float array of 10 elements: 4 Alignment of class basic: 4 Alignment of Empty class: 1
C++ sizeof vs alignof Operators
The sizeof operator returns the size of a variable or data type, telling how much memory it occupies whereas the alignof operator returns the alignment required for a given data-type, telling the boundary on which it stores in memory (for CPU efficiency).
Example
Here is the following example code showcasing the difference between sizeof and alignof operator in C++.
#include <iostream> using namespace std; // a structure with possibility padding struct my_struct { char a; // 1 byte int b; // 4 bytes (here compiler adds padding after 'a') }; int main() { // Single structure cout << "Size of my_struct: " << sizeof(my_struct) << " bytes" << endl; cout << "Alignment of my_struct: " << alignof(my_struct) << " bytes" << endl; // Array of structures my_struct arr[5]; cout << "Size of my_struct[5]: " << sizeof(arr) << " bytes" << endl; cout << "Alignment of my_struct[5]: " << alignof(decltype(arr)) << " bytes" << endl; return 0; }
Output
Size of my_struct: 8 bytes Alignment of my_struct: 4 bytes Size of my_struct[5]: 40 bytes Alignment of my_struct[5]: 4 bytes
Explanation
Inside my_struct, the members are char and int, which require 1 and 4 bytes respectively. So, the actual size is 1 + 4 = 5 bytes, but because of alignment the compiler adds 3 bytes of padding between char and int, making the total size equal to 8 bytes.
Why is 3 bytes of padding added?
Because char has a 1-byte alignment (which can be placed anywhere) but for int, it should be started from the position of multiple of 4 (as 4-byte alignment).
Therefore after char (1 byte) to align int, the compiler adds 3 bytes of padding more.
whereas alignof returns the alignment required, as in this case (comparing char vs int) int is the most strictly aligned member (as it strictly needs 4-byte alignment) therefore alignof operator will return 4 byte, saying that given struct must be aligned on a 4-byte boundary.
Similarly, inside an array of 5 structures, each structure is of 8 bytes (because of padding), therefore total size of total memory occupies 5 Ã 8 = 40 bytes.
Whereas alignof operator will return alignment strictly followed by struct, which will again equal to int (4 bytes) (when comparing both int and char).
Comparison Table
Feature | signof | alignof |
---|---|---|
Purpose | It returns the size (in bytes) | It returns the alignment requirement (in bytes) |
Represents | Total memory it occupies | Memory boundary required to be aligned |
Use-case | Memory allocation, struct size, storage size | Low-level memory alignment, custom allocators |
Evaluated at | Compile-time | Compile-time |
Padding | Yes, it includes internal padding bytes | No, just alignment boundary |