F
Frederick Gotham
It is possible and permissible on a given system, that the alignment
requirements of a type are less than the size of that type. For instance,
here's a hypothetical set-up:
int is 4 bytes, but need only be aligned on a 2 byte boundary.
If we had some sort of "alignof" operator, then we would be able to do
the following:
int main()
{
int array[2];
int *p = array;
/* The following function is defined elsewhere. I use it
here to make this snippet as simple as possible. */
AdvancePointerByBytes( p, alignof(int) );
*p = 5;
}
Over on comp.lang.c, Hallvard B Furuseth devised such an operator, and a
lot of people use it.
I've adapted it to C++, and I've written a sample program consisting of
two headers and one source file. Any thoughts on it?
/* BEGINNING OF advance_ptr.hpp */
#ifndef INCLUDE_ADVANCE_PTR_HPP
#define INCLUDE_ADVANCE_PTR_HPP
#include <cstddef>
template<class T>
inline void AdvancePtr( T * &p, std::size_t const bytes )
{
p = reinterpret_cast< T* >(
const_cast<unsigned char *>(
reinterpret_cast<const unsigned char *>(p) + bytes
)
);
}
template<class T>
inline void RetreatPtr( T * &p, std::size_t const bytes )
{
p = reinterpret_cast< T* >(
const_cast<unsigned char *>(
reinterpret_cast<const unsigned char *>(p) - bytes
)
);
}
#endif
/* END OF advance_ptr.hpp */
/* BEGINNING OF alignof.hpp */
#ifndef INCLUDE_ALIGNOF_HPP
#define INCLUDE_ALIGNOF_HPP
#include <cstddef>
template<class T>
struct StructForAlign {
unsigned char first_byte;
T obj;
};
#define alignof(type) ( offsetof( StructForAlign< type >, obj ) )
#endif
/* END OF alignof.hpp */
/* BEGINNING OF main.cpp */
#include <iostream>
#include "advance_ptr.hpp"
#include "alignof.hpp"
int main()
{
long double array[2];
long double *p = array;
AdvancePtr( p, alignof(long double) );
*p = 23235.2352;
std::cout << *p << '\n';
}
/* END OF main.cpp */
requirements of a type are less than the size of that type. For instance,
here's a hypothetical set-up:
int is 4 bytes, but need only be aligned on a 2 byte boundary.
If we had some sort of "alignof" operator, then we would be able to do
the following:
int main()
{
int array[2];
int *p = array;
/* The following function is defined elsewhere. I use it
here to make this snippet as simple as possible. */
AdvancePointerByBytes( p, alignof(int) );
*p = 5;
}
Over on comp.lang.c, Hallvard B Furuseth devised such an operator, and a
lot of people use it.
I've adapted it to C++, and I've written a sample program consisting of
two headers and one source file. Any thoughts on it?
/* BEGINNING OF advance_ptr.hpp */
#ifndef INCLUDE_ADVANCE_PTR_HPP
#define INCLUDE_ADVANCE_PTR_HPP
#include <cstddef>
template<class T>
inline void AdvancePtr( T * &p, std::size_t const bytes )
{
p = reinterpret_cast< T* >(
const_cast<unsigned char *>(
reinterpret_cast<const unsigned char *>(p) + bytes
)
);
}
template<class T>
inline void RetreatPtr( T * &p, std::size_t const bytes )
{
p = reinterpret_cast< T* >(
const_cast<unsigned char *>(
reinterpret_cast<const unsigned char *>(p) - bytes
)
);
}
#endif
/* END OF advance_ptr.hpp */
/* BEGINNING OF alignof.hpp */
#ifndef INCLUDE_ALIGNOF_HPP
#define INCLUDE_ALIGNOF_HPP
#include <cstddef>
template<class T>
struct StructForAlign {
unsigned char first_byte;
T obj;
};
#define alignof(type) ( offsetof( StructForAlign< type >, obj ) )
#endif
/* END OF alignof.hpp */
/* BEGINNING OF main.cpp */
#include <iostream>
#include "advance_ptr.hpp"
#include "alignof.hpp"
int main()
{
long double array[2];
long double *p = array;
AdvancePtr( p, alignof(long double) );
*p = 23235.2352;
std::cout << *p << '\n';
}
/* END OF main.cpp */