Can an unsigned char array implement malloc?

An unsigned char array can serve as a fixed byte arena, but under C11 it is not a portable, drop-in replacement for malloc when callers expect storage for arbitrary object types. Alignment is only one requirement: a declared array keeps its declared type, while allocated storage follows different object-access rules. You can build a byte-oriented allocator on an array, but casting its address does not make the array into general typed storage.

For a runnable byte-storage example, run the C11 code in your browser without an account or compiler setup.

Can an unsigned char array implement malloc in C?

It depends on what “implement malloc” means. If the allocator manages byte ranges, returns offsets or byte pointers, and stores its own metadata as bytes, an array can be its fixed-capacity arena. If it promises clients they can use returned memory as arbitrary struct, double, or other typed objects, a declared character array is not automatically equivalent to the storage returned by malloc under C11.

That distinction matters for an educational allocator too: a correct block layout does not, by itself, settle which typed accesses C permits.

Why doesn't aligning the array solve it?

C11 lets you request stronger alignment for a declared array:

#include <stddef.h>

_Alignas(max_align_t)
static unsigned char arena[
    4096
];

This requests alignment at least as strict as max_align_t. malloc also promises alignment suitable for objects with fundamental alignment requirements; see C11 draft §7.22.3. But alignment answers where an object may start. It does not change the type declared for arena, or make a typed object exist inside its elements.

The diagram separates those two questions. The array can start at a suitably aligned address and still be a declared unsigned char[4096].

Comparison of an aligned declared unsigned-char array and malloc storage: alignment is separate from declared-type rules.

An allocator must also align each returned block, not just the arena base. Its header footprint, payload offset, and block stride all affect whether later payloads stay aligned. Heapcraft’s allocator alignment explanation works through those offsets.

What type does each kind of storage have?

C11’s effective-type rules distinguish objects that have a declared type from allocated objects that do not. For an object with a declared type, that declared type governs access to its stored value; allocated storage has no declared type, and its effective type is determined by the rules in §6.5 paragraphs 6–7.

A character-type lvalue may inspect an object’s representation byte by byte. The permission goes in that direction: it does not generally let you access a declared character array by casting it to an unrelated structure pointer and dereferencing it. A cast changes the pointer expression, not the array’s declared type. If the resulting address also fails the target type’s alignment requirement, that is a separate problem.

For example, this stores a header’s object representation in an array and copies it back into a real header object:

#include <stddef.h>
#include <string.h>

struct block_header {
    size_t span;
};
unsigned char bytes[
    sizeof(struct block_header)
];

struct block_header h = {
    .span = 64
};
memcpy(bytes, &h, sizeof h);

struct block_header copy;
memcpy(&copy, bytes,
       sizeof copy);

The array holds bytes representing the header; the memcpy does not declare a struct block_header inside bytes. For standards wording and compiler practice, see the C11 draft and WG14’s N2012 discussion of character-array storage. N2012 records that some uses are widespread and that the object-model question has been discussed; it is not a blanket guarantee that every compiler treats a declared byte array like malloc storage.

When is a byte-array arena a good fit?

A declared character array is a straightforward choice when your allocator’s contract stays byte-oriented. For example, it can manage fixed spans, return offsets into the arena, or copy metadata representations with memcpy. You can also keep metadata in separately declared struct objects, rather than overlaying a structure on the array.

The runnable C11 byte-arena example runs in the browser without an account or compiler setup. You can also read, copy, or download its standalone source and compile it locally; the page includes 14 checks for metadata round trips, bounds, and payload preservation. The example copies metadata with memcpy between the byte array and real header objects. It demonstrates byte storage, not allocation for arbitrary typed objects or a general-purpose malloc implementation.

Avoid treating this cast as a portable shortcut to a general-purpose heap:

struct block_header *header =
    (struct block_header *)
        arena;

Before dereferencing such a pointer, you would need more than a correctly aligned address: the C object model must permit the typed access too. A header cast that happens to work on one compiler is not evidence that the array has become allocated storage.

If your allocator must hand clients storage for typed objects, one conventional design is to obtain a larger backing allocation from malloc and suballocate it. Then the suballocator still has to keep every result within bounds, nonoverlapping, and properly aligned; it is managing malloc-provided storage, not replacing the system allocation primitive. If you keep a declared array as backing storage, define a byte-oriented API or rely only on a documented implementation-specific contract.

The practical rule

Use an unsigned char array for an explicit byte arena; do not assume alignment alone makes it a portable malloc substitute for arbitrary typed objects. Keep byte layout, alignment, and valid typed access as three separate questions in your allocator’s contract and tests.

Heapcraft’s free first-lesson preview lets you try those layout checks directly: implement align_offset and arena_take to reserve aligned slices in a 64-byte arena, then run seven C tests. Try the free lesson without registering. Guest code and completion are unsaved; registration is needed only to save work and continue through the preview. The full guided course is separate and unavailable, with no launch date; join its waitlist if you want updates.

Can an unsigned char array implement malloc? · Heapcraft