提问人:programme3219873 提问时间:9/9/2021 最后编辑:Jonathan Lefflerprogramme3219873 更新时间:9/9/2021 访问量:68
我如何在动态分配的结构中释放()动态分配的数组
How would I free() a dynamically allocated array in a dynamically allocated struct
问:
我编写了一个程序来模拟“队列”数据结构——代码如下:
queue.h
:
#ifndef QUEUE
#define QUEUE
typedef struct Q
{
int *queue;
int size;
int elements;
int end;
int beg;
} QueueType;
QueueType *Create(int n);
int Insert_end(QueueType *q_ptr, int n);
int Remove_Beg(QueueType *q_ptr);
int Ret_First(QueueType *q_ptr);
int Ret_Last(QueueType *q_ptr);
#endif
queue.c
:
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
QueueType *Create(int n)
{
QueueType *ptr = malloc(sizeof(QueueType));
ptr->queue = malloc(sizeof(int) * n);
ptr->size = n;
ptr->beg = n-1;
ptr->end = n-2;
ptr->elements = 0;
return ptr;
}
int Insert_end(QueueType *q_ptr, int n) // inserts data at end of queue
{
if (!(q_ptr->elements == q_ptr->size)) // "elements" is number of valid data in the queue, size is the size of the queue.
{
if (q_ptr->elements == 0) // if the queue is empty
{
q_ptr->queue[q_ptr->size-1] = n;
q_ptr->elements++;
}
else // if the queue is not empty
{
q_ptr->queue[q_ptr->end] = n;
q_ptr->end = q_ptr->end-1;
q_ptr->elements++;
}
}
else // if the queue is full
{
printf("Queue Full");
}
}
int Remove_Beg(QueueType *q_ptr) // removes beginning element of queue
{
printf("\n");
for (int *start = &(q_ptr->queue[q_ptr->beg-1]); start >= &(q_ptr->queue[q_ptr->end-1]); start--)
*(start+1) = *start;
// goes from start of queue to end and moves values along so the first value gets overwritten eventually (basically pops the first value and moves each value along)
}
int Ret_First(QueueType *q_ptr) // returns first queue value
{
return q_ptr->queue[q_ptr->beg];
}
int Ret_Last(QueueType *q_ptr) // returns last queue value
{
return q_ptr->queue[q_ptr->end];
}
main.c
:
#include <stdio.h>
#include "queue.h"
int main()
{
QueueType *ptr = Create(10);
Insert_end(ptr,2);
Insert_end(ptr,4);
Insert_end(ptr,6);
Insert_end(ptr,8);
Insert_end(ptr,10);
Insert_end(ptr,12);
Insert_end(ptr,14);
Insert_end(ptr,16);
Insert_end(ptr,18);
Insert_end(ptr,20);
Remove_Beg(ptr);
free(ptr->queue);
free(ptr);
return 0;
}
我的问题是,在运行 valgrind 检查内存泄漏和错误后,还剩下 40 个字节:
==4007== LEAK SUMMARY:
==4007== definitely lost: 40 bytes in 1 blocks
==4007== indirectly lost: 0 bytes in 0 blocks
==4007== possibly lost: 0 bytes in 0 blocks
==4007== still reachable: 0 bytes in 0 blocks
==4007== suppressed: 0 bytes in 0 blocks
我只能假设这是来自使用该函数分配的 10 个整数数组。main 函数给出了错误,那么我该如何释放分配的数组以及存储数组的实际结构体呢?Create()
free()
答: 暂无答案
评论
-g
Remove_Beg()
int
void
Queue Full
stderr
stdout
fprintf(stderr, "Queue Full\n");
Insert_end()
Remove_Beg()
Remove_Beg()
Remove_Beg
beg
end
Remove_Beg
-fsanitize-address