Queue中如何释放双指针【C语言】?
How to free double pointer in Queue [C language]?
调用 deleteQueue 时:
Exception thrown at 0x00007FF8E378169C (ucrtbased.dll) in LoadBalancer.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
创建队列函数:
queue_t* createQueue(unsigned capacity){
queue_t* queue = (queue_t*)malloc(sizeof(queue_t));
if (queue == NULL)
{
return NULL;
}
queue->capacity = capacity;
queue->array = (char**)malloc(sizeof(char*) * queue->capacity);
queue->size = 0;
queue->front = 0;
queue->rear = -1;
InitializeCriticalSection(&(queue->cs));
return queue;
}
排队:
void enqueue(queue_t* queue, char* string){
EnterCriticalSection(&(queue->cs));
if (isFull(queue)) {
LeaveCriticalSection(&(queue->cs));
return;
}
queue->rear = (queue->rear + 1) % queue->capacity;
queue->size = queue->size + 1;
char* ptr = (char*)malloc(strlen(string)+1);
memcpy(ptr, string, strlen(string)+1);
queue->array[queue->rear] = ptr;
LeaveCriticalSection(&(queue->cs));
return;
}
删除队列函数:
void deleteQueue(queue_t* queue){
DeleteCriticalSection(&(queue->cs));
if (queue != NULL)
{
for (int i = 0; i < queue->capacity; i++) {
free(queue->array[i]);
}
free(queue->array);
free(queue);
}
}
我是否也应该在出队程序中断时释放指针并出现相同的异常?
char* dequeue(queue_t* queue){
EnterCriticalSection(&(queue->cs));
if (isEmpty(queue)) {
LeaveCriticalSection(&(queue->cs));
return NULL;
}
char* temp = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
LeaveCriticalSection(&(queue->cs));
return temp;
}
对于初学者,您分配了一个具有不确定值的指针数组。
queue->array = (char**)malloc(sizeof(char*) * queue->capacity);
所以在函数中使用这个循环 deleteQueue
for (int i = 0; i < queue->capacity; i++) {
free(queue->array[i]);
}
可以调用未定义的行为。您应该使用空指针初始化数组。例如,您可以使用函数 calloc
.
而不是使用函数 malloc
其次在函数内 dequeue
在这条语句之后
char* temp = queue->array[queue->front];
你应该写
queue->array[queue->front] = NULL;
用户有责任释放返回的字符串。
调用 deleteQueue 时:
Exception thrown at 0x00007FF8E378169C (ucrtbased.dll) in LoadBalancer.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
创建队列函数:
queue_t* createQueue(unsigned capacity){
queue_t* queue = (queue_t*)malloc(sizeof(queue_t));
if (queue == NULL)
{
return NULL;
}
queue->capacity = capacity;
queue->array = (char**)malloc(sizeof(char*) * queue->capacity);
queue->size = 0;
queue->front = 0;
queue->rear = -1;
InitializeCriticalSection(&(queue->cs));
return queue;
}
排队:
void enqueue(queue_t* queue, char* string){
EnterCriticalSection(&(queue->cs));
if (isFull(queue)) {
LeaveCriticalSection(&(queue->cs));
return;
}
queue->rear = (queue->rear + 1) % queue->capacity;
queue->size = queue->size + 1;
char* ptr = (char*)malloc(strlen(string)+1);
memcpy(ptr, string, strlen(string)+1);
queue->array[queue->rear] = ptr;
LeaveCriticalSection(&(queue->cs));
return;
}
删除队列函数:
void deleteQueue(queue_t* queue){
DeleteCriticalSection(&(queue->cs));
if (queue != NULL)
{
for (int i = 0; i < queue->capacity; i++) {
free(queue->array[i]);
}
free(queue->array);
free(queue);
}
}
我是否也应该在出队程序中断时释放指针并出现相同的异常?
char* dequeue(queue_t* queue){
EnterCriticalSection(&(queue->cs));
if (isEmpty(queue)) {
LeaveCriticalSection(&(queue->cs));
return NULL;
}
char* temp = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
LeaveCriticalSection(&(queue->cs));
return temp;
}
对于初学者,您分配了一个具有不确定值的指针数组。
queue->array = (char**)malloc(sizeof(char*) * queue->capacity);
所以在函数中使用这个循环 deleteQueue
for (int i = 0; i < queue->capacity; i++) {
free(queue->array[i]);
}
可以调用未定义的行为。您应该使用空指针初始化数组。例如,您可以使用函数 calloc
.
malloc
其次在函数内 dequeue
在这条语句之后
char* temp = queue->array[queue->front];
你应该写
queue->array[queue->front] = NULL;
用户有责任释放返回的字符串。