小编Mis*_*lab的帖子

c指针减少安全/不安全?

分析一些代码:

static volatile UCHAR *pucSndBufferCur;

eMBErrorCode eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
{
    if( eRcvState == STATE_RX_IDLE )
    {
        /* First byte before the Modbus-PDU is the slave address. */
        pucSndBufferCur = ( UCHAR * ) pucFrame - 1;

        /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
        pucSndBufferCur[0] = ucSlaveAddress;
Run Code Online (Sandbox Code Playgroud)

看起来不安全,如果地址中的内存(pucFrame - 1)已用于其他变量并覆盖它可能会导致故障.

您怎么看?这样的代码可以使用还是错误的方式,永远不应该使用?

c pointers unsafe-pointers

3
推荐指数
1
解决办法
317
查看次数

标签 统计

c ×1

pointers ×1

unsafe-pointers ×1