C++中类成员使用前需要初始化的重要性

时间:2023-05-12 17:45:25

今天写程序的时候,创建了一个结构体:

struct BufferObj {
char* buf;
int bufLen;
SOCKADDR_STORAGE addr;
int addrLen;
struct BufferObj* next;
};

该结构体有一个next指针,本意是这个指针初始的时候应该为NULL,如果有新的BufferObj添加,这个next指针就会指向这个新添加的BufferObj,其实就是一个链表。

程序中有一个生产者线程使用enqueueBufferObj方法向链表里面添加BufferObj:

void enqueueBufferObj(ConnectionObj* connObj, BufferObj* bufferObj) {
    EnterCriticalSection(&connObj->sendRecvQueueCriticalSection);

    

    if (connObj->pendingSendHead == NULL) {
        connObj->pendingSendHead = connObj->pendingSendTail = bufferObj;
    } else {
        
        connObj->pendingSendTail->next = bufferObj;
        connObj->pendingSendTail = bufferObj;
    }

    LeaveCriticalSection(&connObj->sendRecvQueueCriticalSection);
}

还有一个消费者线程从这个链表里面取BufferObj:

BufferObj* dequeueBufferObj(ConnectionObj* connObj) {
    BufferObj* res = NULL;

    EnterCriticalSection(&connObj->sendRecvQueueCriticalSection);

    if (connObj->pendingSendTail != NULL) {
        res = connObj->pendingSendHead;
        connObj->pendingSendHead = connObj->pendingSendHead->next;
        if (connObj->pendingSendTail == res) {
            connObj->pendingSendTail = NULL;
        }
    }
    LeaveCriticalSection(&connObj->sendRecvQueueCriticalSection);
    return res;
}

 其中,ConnectionObj结构体如下:

struct ConnectionObj {
    SOCKET s;
    HANDLE hRecvSemaphore;
    struct BufferObj* pendingSendHead;
    struct BufferObj* pendingSendTail;
    CRITICAL_SECTION sendRecvQueueCriticalSection;
};

刚开始的时候,由于程序中没有显示的将BufferObj的next属性初始化NULL,导致程序运行到enqueueBufferObj方法时总是出现指针违法访问:

connObj->pendingSendTail->next = bufferObj;

上面就是出错的地方。程序中对于ConnectionObj中的pendingSendHead和pendingSendTail都已经显示初始化为NULL了。经过查找发现,是因为程序中没有显式对

BufferObj中next进行NULL初始化,从而当消费者线程从队列中BufferObj之后,会重新对队列头进行初始化,该代码在dequeueBufferObj中:

connObj->pendingSendHead = connObj->pendingSendHead->next;

此时,如果BufferObj中的next显示初始化为了NULL,那么connObj->pendingSendHead的值应该为NULL,但是程序中没有对next进行显式初始化,所以,此时,

connObj->pendingSendHead的值为一个随机值,这导致生产者线程使用enqueueBufferObj在向队列中添加新BufferObj时出错:

if (connObj->pendingSendHead == NULL) {//如果next显式初始化了,这个条件检测应该成立
        connObj->pendingSendHead = connObj->pendingSendTail = bufferObj;
    } else {//但是由于next没有显示初始化,导致pendingSendHead的值不是NULL,而是一个随机的,因此程序错误的运行到这里,出现上述错误
        
        connObj->pendingSendTail->next = bufferObj;
        connObj->pendingSendTail = bufferObj;
    }

 

在简单的程序中,这中错误查找起来可能不是问题,但是如果程序很复杂,查找这种错误就会很浪费时间。因此,为了安全起见,以后对于C++中的结构体,类成员,在使用前,还是先进行初始化后为好。