Implementation:ClickHouse ClickHouse Unaligned
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Memory, Performance |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Safe unaligned memory access functions for reading and writing data.
Description
Provides unalignedLoad/Store functions that work correctly even when addresses aren't aligned, using memcpy to avoid UB.
Usage
Use when reading/writing data at arbitrary memory locations, parsing binary formats, or implementing serialization.
Code Reference
Source Location
- Repository: ClickHouse
- File: base/base/unaligned.h
- Lines: 1-93
Signature
template <typename T>
inline T unalignedLoad(const void * address);
template <typename T>
inline void unalignedStore(void * address, const typename std::enable_if<true, T>::type & src);
template <std::endian endian, typename T>
inline T unalignedLoadEndian(const void * address);
template <std::endian endian, typename T>
inline void unalignedStoreEndian(void * address, T & src);
template <typename T>
inline T unalignedLoadLittleEndian(const void * address);
template <typename T>
inline void unalignedStoreLittleEndian(void * address, const typename std::enable_if<true, T>::type & src);
Import
#include <base/unaligned.h>
Usage Examples
#include <base/unaligned.h>
char buffer[100];
char* ptr = buffer + 1; // Misaligned
// Safe unaligned access
uint64_t value = unalignedLoad<uint64_t>(ptr);
unalignedStore<uint32_t>(ptr + 8, 12345);
// Endian-specific loads
uint32_t le = unalignedLoadLittleEndian<uint32_t>(ptr);
uint32_t be = unalignedLoadBigEndian<uint32_t>(ptr);
// Store with endianness
unalignedStoreLittleEndian<uint64_t>(ptr, 0x123456789ABCDEF0ULL);
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment