はList
ADT、List ADT - 構造体ポインタとそのメンバを解放するには?
typedef struct List{
void **array;
/* Following members for Housekeeping - Array enhancement*/
int lastItemPosition;
int size;
}List;
#define INITIAL_LIST_SIZE 50
createList
操作だけでvoid*
の配列を解放するために期待しfree(*(list->array))
にしようとしますが、いないオブジェクトがlist
List *createList(List *list, Op opType){
List *listPointer = (List *)malloc(sizeof(List));
void *accumulator = NULL;
if(opType == CREATE_NEW_LIST){
accumulator = malloc(INITIAL_LIST_SIZE*sizeof(void*));
listPointer->array = &accumulator;
/* Is it safe to initialise zero to element of array of void* pointers? */
listPointer->array = memset(listPointer->array, 0, INITIAL_LIST_SIZE*sizeof(void *));
listPointer->lastItemPosition = -1;
listPointer->size = INITIAL_LIST_SIZE;
}else if(opType == DOUBLE_THE_LIST){
accumulator = malloc(2*(list->size)*sizeof(void *));
listPointer->array = &accumulator;
/* Performing shallow copy, Is deep copy required? */
listPointer->array = memcpy(listPointer->array, list->array, list->size*sizeof(void*));
listPointer->lastItemPosition = list->lastItemPosition;;
listPointer->size = 2*(list->size);
free(*(list->array)); // How to free list pointer and its members?
}else if(opType == HALF_THE_LIST){
accumulator = malloc(((list->size)/2)*sizeof(void *));
listPointer->array = &accumulator;
/* Performing shallow copy, Is deep copy required? */
listPointer->array = memcpy(listPointer->array, list->array, (list->size/2)*sizeof(void *));
listPointer->lastItemPosition = list->lastItemPosition;
listPointer->size = (list->size)/2;
free(*(list->array)); // How to free list pointer and its members?
}
return listPointer;
}
の簡易コピーをとり、各
void*
listPointer
ために指摘しています
次のリスト操作は、List
void insertItem(List *, void *newItem);
void deleteItem(List *, int listIndex);
ユーザーアクセス、
を行っている/* main.c */
#include"list.h"
int main(void){
List *arrayList = createList((List *)NULL, CREATE_NEW_LIST);
if (arrayList == (List *)NULL){
fprintf(stderr, "Unable to createList() \n");
exit(1); //Nothing else to do without arrayList
}
/* Objects should be on heap */
int *object = malloc(sizeof(int));
*object = 650;
insertItem(arrayList, object);
}
質問:
あなたは明確にしてくださいすることができ、自由に(*(リスト - >配列を何) )私のコードでは何ですか?
を? – pm100
カプセル化の面が注意されています。それを無視してください。フルコード[こちら](https://github.com/shamhub/Computing/tree/master/2_List/Implementation_C) 'linkedListImpl.c'はまだケアされていません – overexchange
@ pm100確かに、この状況で' free'を使う方法はありますか?リストの半分を強化するために浅いコピーを行っているので – overexchange