cnullembedded8-bit68hc11

Address 0 being overwritten by NULL pointer on HCS08


On my 8-bit Freescale HCS08 micro, whenever I call a library function that returns values to pointers I pass it, and I don't really want them, I threw in a NULL, e.g.

UART_SendBlock((char *)txstr, strlen(txstr), NULL);

The type of the last argument is uint16_t * and returns the number of characters actually sent, a value I don't care about.

However, I was having problems with port A on my micro being hosed every time that function was called, and debugging pointed me to that argument screwing things up. Port A's configuration registers live at addresses 0x0000 and 0x0001, which is where NULL (aka (void *)0) points. I thought NULL was somehow magic where it wouldn't actually do anything, but it doesn't seem so.

My workaround feels really hack:

#define MNUL (void *)(&mynull)
uint32_t mynull;

Is there a better solution? I tried defining MNUL to an unused segment of memory on my processor, but that causes an immediate reset.


Solution

  • Unless the documentation for UART_SendBlock states that passing it a NULL pointer is ok , you probably get undefined behavior if you do - and in this case it seems the function just writes to the address you pass it, address 0 - which would go under "undefined behavior".

    Just call it with a parameter, there's no reason to be "clever" about it.

    uint16_t unused_16;
    UART_SendBlock((char *)txstr, strlen(txstr), &unused_16);
    

    If you are resource constrained, make unused_16 a global and reuse it in other places