Question

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.

Was it helpful?

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

OTHER TIPS

The function is trying to write a value into wherever the last argument points. You're passing in NULL (0x0000), so the result is being written to the memory location 0x0000, or your port A. Usually (in general programming) the function would check the argument and not try to write to the location if it was NULL, but it appears this function doesn't follow that convention. You need to allocate a variable for the function to write its result to, even if you aren't going to use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top