我的ArrayList的文件:當我把10個對象在它çArrayList的崩潰
#include <stdint.h>
#include <malloc.h>
#include <memory.h>
#include <stdio.h>
#include "ArrayList.h"
typedef struct ArrayList {
uint32_t size;
void **data;
uint32_t capacity;
} ArrayList;
ArrayList *arrayList_construct(uint32_t initialCapacity) {
if (initialCapacity < 10) {
initialCapacity = 10;
}
ArrayList *arrayList = malloc(sizeof(ArrayList));
arrayList->size = 0;
arrayList->capacity = initialCapacity;
arrayList->data = malloc(initialCapacity * sizeof(void *));
return arrayList;
}
void arrayList_add(ArrayList *arrayList, void *item) {
ensureCapacity(arrayList);
*(arrayList->data + arrayList->size) = item;
arrayList->size++;
}
void arrayList_set(ArrayList *arrayList, uint32_t index, void *value) {
*(arrayList->data + index) = value;
}
void *arrayList_get(ArrayList *arrayList, uint32_t index) {
return *(arrayList->data + index);
}
void arrayList_remove(ArrayList *arrayList, uint32_t index) {
memcpy(arrayList->data + index, arrayList->data + index + 1, (arrayList->size - index - 1) * sizeof(void *));
free(*(arrayList->data + arrayList->size-- - 1));
}
uint32_t arrayList_getSize(ArrayList *arrayList) {
return arrayList->size;
}
void arrayList_destruct(ArrayList *arrayList) {
free(arrayList->data);
free(arrayList);
}
static void ensureCapacity(ArrayList *arrayList) {
if (arrayList->size >= arrayList->capacity) {
uint32_t oldCapacity = arrayList->capacity;
uint32_t newCapacity = oldCapacity + (oldCapacity >> 1) * 2;
arrayList->capacity = newCapacity;
void **newSpace = malloc(sizeof(void *) * newCapacity);
void **oldSpace = arrayList->data;
arrayList->data = memcpy(newSpace, oldSpace, oldCapacity);
free(oldSpace);
}
}
int main() {
ArrayList *arrayList = arrayList_construct((uint32_t) 10);
int *array = malloc(100 * sizeof(int));
for (int i = 0; i < 100; ++i) {
*(array + i) = i;
arrayList_add(arrayList, array + i);
}
for (int i = 0; i < arrayList_getSize(arrayList); i++) {
printf("[%p]: %d\n", arrayList_get(arrayList, i), *(int *) arrayList_get(arrayList, i));
}
return 0;
}
ArrayList中正常工作。 但是當我在其中放置更多的對象時,它崩潰(內存訪問衝突)(100)。 它打印前2個對象,然後崩潰。
我該如何解決這個問題?
你爲什麼不使用['realloc'](http://en.cppreference.com/w/c/memory/realloc)? –
你的堆有多大? –