6 MArray
myarray_new(int size
, int incr
, int elsize
) {
7 MArray myarray
= malloc(sizeof(struct myarray
));
8 myarray
->elements
= calloc(size
, sizeof(void *));
10 myarray
->maxlen
= size
;
12 myarray
->elsize
= elsize
;
17 static void myarray_enlarge(MArray array
) {
18 array
->maxlen
+= array
->incr
;
19 array
->elements
= realloc(array
->elements
, array
->maxlen
*sizeof(void *));
22 void _myarray_insert(MArray array
, void *element
, int index
) {
23 if (index
< 0 || index
> array
->len
) {
24 fprintf(stderr
, "Array index out of bounds %s:%d. Index: %d. Array size: %d\n", __FILE__
, __LINE__
, index
, array
->len
);
27 if (array
->len
== array
->maxlen
) {
28 myarray_enlarge(array
);
30 void *val
= malloc(array
->elsize
);
31 memcpy(val
, element
, array
->elsize
);
32 memmove(&array
->elements
[index
+1], &array
->elements
[index
], (array
->len
- index
)*sizeof(void *));
33 array
->elements
[index
] = element
;
38 void myarray_remove_index(MArray array
, int index
) {
39 free(array
->elements
);
40 memmove(&array
->elements
[index
], &array
->elements
[index
+1], (array
->len
- index
- 1)*sizeof(void *));
44 void myarray_free(MArray array
) {
46 for (i
= 0; i
< array
->len
; i
++) {
47 free(myarray_get(array
, void *, i
));
49 free(array
->elements
);
53 void myarray_reset(MArray array
) {