Resources | Resources |



Do not zero-fill memory allocations

Zero-fill of a memory allocation is rarely necessary. Zero-fill of a memory allocation is usually done to avoid writing more code, at the expense of performance.

When zero-filled memory is used, an application allocates zero-filled memory of the appropriate size, then write the characters of the string until done. The memory is zero-filled so the string is null-terminated. This approach touches each byte containing data twice. This requires more writes, and can force useful data out of the processor's data cache, forcing a re-fetch when that data is required.

When non-zero-filled memory is used, the application allocates non-zero-filled memory of the appropriate size, then writes the characters of the string. When the last character is written, the application writes a null in the next character position. This approach only touches the bytes that contain relevant data.

By default, Brew MP's MALLOC() zero-fills its allocations. To get raw memory, you need to OR the flag ALLOC_NO_ZMEM into the size being requested. Rather than using MALLOC(), you can write and use your own allocation function or macro to set the flag.