Why Your C Pointer Code Crashes on MCU: Memory Alignment Pitfalls and Fixes
This article explains how improper pointer usage and memory‑alignment violations in C can cause crashes on microcontrollers, demonstrates the issue with a concrete code example, and provides portable solutions such as byte‑wise access or memcpy to avoid unaligned accesses.
Introduction
In C, a pointer stores a memory address and provides dynamic access to storage locations. Understanding pointer semantics and memory‑alignment rules is essential because many CPUs can only read or write data at addresses that are multiples of the data size (word, half‑word, etc.).
Problem Example
The following program assumes an array whose base address is 0x00004000 (aligned to 4‑byte boundaries). It writes a single byte and a 16‑bit value at successive offsets, then prints the first three bytes of the buffer.
static unsigned char sg_arrBuf[100];
int main()
{
memset(sg_arrBuf, 0, sizeof(sg_arrBuf));
uint8_t *pucVal = (uint8_t *)&sg_arrBuf[0]; // address 0x00004000
uint16_t *puiVal = (uint16_t *)&sg_arrBuf[1]; // address 0x00004001 (misaligned)
*pucVal = 20; // 0x14
*puiVal = 2000; // 0x07D0
printf("HEX: ");
for (int i = 0; i < 3; i++)
{
printf("%02x ", sg_arrBuf[i]);
}
printf("
");
return 0;
}Expected Output on a Little‑Endian PC
HEX: 14 d0 07
Why the Result May Differ on an MCU
Most microcontroller CPUs require that accesses to multi‑byte objects be aligned to their size. The pointer puiVal points to address 0x00004001, which is not a multiple of 2. On architectures that enforce alignment, reading or writing through this pointer triggers a bus fault, causing the program to crash or behave unpredictably.
Unaligned accesses can raise exceptions or produce incorrect data because the hardware fetches memory in word‑sized chunks.
Preventive Measures and Fixes
To make the code portable across PCs and MCUs, avoid unaligned pointer casts. Two common approaches are:
Operate on byte‑sized variables only.
Use memcpy to copy the values into the buffer, which the compiler implements safely.
Rewritten example using memcpy:
static unsigned char sg_arrBuf[100];
int main()
{
memset(sg_arrBuf, 0, sizeof(sg_arrBuf));
uint8_t ucVal = 20;
uint16_t uiVal = 2000;
memcpy(&sg_arrBuf[0], &ucVal, 1);
memcpy(&sg_arrBuf[1], &uiVal, 2);
printf("HEX: ");
for (int i = 0; i < 3; i++)
{
printf("%02x ", sg_arrBuf[i]);
}
printf("
");
return 0;
}This version stores the byte and the 16‑bit value via byte‑wise copies, guaranteeing that the underlying memory accesses are aligned and safe on any target platform.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
