I'm trying to write an "ArrayList" program (Similar to the Java ArrayList
) that will expand automaticly using realloc
, so that the programmer won't have to worry about storage space in arrays. This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define ARR_DEFAULT_SIZE 20
#define INCR 10
#define ARRTYPE char // Files using this #undef this macro and provide their own type
typedef struct {
ARRTYPE *arr;
long size;
long nextincr;
} arrlst;
arrlst *nlst(void);
void add(arrlst *, ARRTYPE);
ARRTYPE elmat(arrlst *, long);
int main(int argc, char **argv) {
arrlst *lst = nlst();
add(lst, 'h');
}
arrlst *nlst() {
arrlst lst = { malloc(ARR_DEFAULT_SIZE), 0, ARR_DEFAULT_SIZE };
arrlst *lstptr = &lst;
return lstptr;
}
void add(arrlst *lst, ARRTYPE elm) {
if (lst->size >= lst->nextincr) {
ARRTYPE *tmp = lst->arr;
lst->nextincr += INCR;
lst->arr = realloc(lst->arr, lst->nextincr);
for (int i = 0; i < sizeof tmp; i++)
lst->arr[i] = tmp[i];
}
lst->arr[lst->size++] = elm;
}
ARRTYPE elmat(arrlst *lst, long at) {
if (lst->size < at)
strerror(14);
return lst->arr[at];
}
My problem is that whenever I run this, calling add()
produces a segfault, and since most of the code in add()
is skipped on the first time calling it, the line of error must be:
lst->arr[lst->size++] = elm;
And I don't know why this would segfault. Please help!