이 코드에서는 Python 목록이나 Java ArrayList와 비슷한 목록 컬렉션을 구현하는 C 모듈을 개발하려고합니다. 그러나 나는 다음에 문제가있어,목록에 할당 된 메모리를 해제하지 않고 매개 변수가 가리키는 목록을 비우는 방법은 무엇입니까?
이#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>`
#include "array_list.h"
/* Construct a new, empty list, and return a pointer to it
* Parameter capacity is the # of elements that can be stored in the list.
* Terminate the program via assert if capacity is <= 0.
* Terminate the program via assert if memory for the list cannot be
* allocated from the heap.
*/
intlist_t *intlist_construct(int capacity)
{
int *pa;
pa = malloc(capacity * sizeof(int));
assert(pa != NULL);
intlist_t *list = malloc(sizeof(intlist_t));
assert(list != NULL);
list->capacity = capacity;
list->size = 0;
list->elems = pa;
return list;
}
/* Destroy the list pointed to by parameter list, deallocating all memory
* that was allocated from the heap.
* Terminate the program via assert if list is NULL.
*/
void intlist_destroy(intlist_t *list)
{
assert(list != NULL);
free(list->elems); /* Return the array to the heap. */
free(list); /* Return the structure to the heap. */
}
/* Print the list pointed to by parameter list to the console.
* Terminate the program via assert if list is NULL.
*/
void intlist_print(const intlist_t *list)
{
assert(list != NULL);
if (list->size == 0)
{
printf("[]\n");
}
printf("[");
for (int i = 0; i < list->size; i++)
{
if (i != list->size - 1)
{
printf("%d ", list->elems[i]);
}
else
{
printf("%d]", list->elems[i]);
}
}
}
/* Insert element at the end of the list pointed to by list.
* Return true if element is appended; otherwise return false
* (which indicates that the list is full.)
* Terminate the program via assert if list is NULL.
*/
_Bool intlist_append(intlist_t *list, int element)
{
assert(list != NULL);
if (list->size < list->capacity)
{
list->elems[list->size] = element;
list->size++;
return true;
}
else
{
return false;
}
}
/* Return the maximum number of integers that can be stored in the list
* pointed to by parameter list.
* Terminate the program via assert if list is NULL.
*/
int intlist_capacity(const intlist_t *list)
{
assert(list != NULL);
return list->capacity;
}
/* Return the number of integers in the list pointed to by parameter list.
* Terminate the program via assert if list is NULL.
*/
int intlist_size(const intlist_t *list)
{
assert(list != NULL);
return list->size;
}
/* Return the element located at the specified index, in the list
* pointed to by parameter list.
* Terminate the program via assert if list is NULL,
* or if index is not in the range 0 .. intlist_size() - 1.
*/
int intlist_get(const intlist_t *list, int index)
{
assert(list != NULL);
return list->elems[index];
}
/* Store element at the specified index, in the list pointed to by
* parameter list. Return the integer that was previously
* stored at that index.
* Terminate the program via assert if list is NULL,
* or if index is not in the range 0 .. intlist_size() - 1.
*/
int intlist_set(intlist_t *list, int index, int element)
{
assert(list != NULL);
int previous;
previous = list->elems[index];
list->elems[index] = element;
return previous;
}
내 코드의 시작 부분은 작동이 내가 생성 한 코드가
typedef struct {
int *elems; // Pointer to backing array.
int capacity; // Maximum number of elements in the list.
int size; // Current number of elements in the list.
} intlist_t;
:
이 구조체 intlist_t있다 . intlist_removal이 반환 될 때 0 개의 정수를 포함하는 목록을 반환하고 싶습니다. 목록을 비울 때 제대로 작동하지 않습니다. 코드에서
/* Empty the list pointed to by parameter list.
* Memory allocated to the list is not freed, so the emptied list can
* continue to be used.
* Terminate the program via assert if list is NULL.
*/
void intlist_removeall(intlist_t *list)
{
assert(list != NULL);
free(list);
}
왜 제대로 작동하지 않는지 더 설명 할 수 있습니까? – Grantly
내가 입력 할 때 : list = intlist_construct (10); 그래서 용량은 10이고 목록을 [0, 2, 4, 6, 8]로 초기화합니다. intlist_removeall (list)를 호출하면 결과는 0이어야합니다. 메모리 – student17
게시물을 추가 정보로 편집하십시오 – Grantly