</>EmbeddedInterview

Implement your own memset().

Easy"Coding: Memory & String Functions"
#include <stddef.h>
#include <stdint.h>

void *my_memset(void *dst, int c, size_t n)
{
    uint8_t *d = dst;
    uint8_t b = (uint8_t)c;
    while (n && ((uintptr_t)d & 3u)) { *d++ = b; n--; }      /* align */
    uint32_t w = 0x01010101u * b;                             /* replicate the byte */
    uint32_t *dw = (uint32_t *)(void *)d;
    while (n >= 4) { *dw++ = w; n -= 4; }
    d = (uint8_t *)dw;
    while (n--) *d++ = b;
    return dst;
}

Points: c is converted to unsigned char; memset(p, 1, n) on an int array does not set each int to 1 (it sets every byte to 0x01, giving 0x01010101). To clear secrets, the compiler may delete a memset before free/return as a "dead store": use memset_s, explicit_bzero, or a volatile pointer loop.