-1

I am trying to implement a java-like arraylist in C and now I am trying to make it generic. I am new to C and pointer arithmetic but I tried using void* pointer for container, then element size(for size of data type), capacity and size. I am trying to debug my code and it is giving a random value on the screen. I cant seem to find what is wrong with my add method ? and how should I go about the printlist method as we are not sure about the data type. I think my printLIst is wrong. I am new to C and playing around for educational purposes and any help is appreciated. Thank you

____________________________________________________________________
#ifndef ARRAYLIST_H
#define ARRAYLIST_H

typedef struct ArrayList ArrayList;
typedef int bool;
#define false 0
#define true 1
struct ArrayList {
    void *con;
    int elementSize;
    int numElements;
    int conSize;
};

ArrayList *createArrayList(int );
int remove(ArrayList *, int);
void freeArrayList(ArrayList *);
int add(ArrayList *, void*);
void *getAtIndex(ArrayList *, int);
void printList(ArrayList *);
void resize(ArrayList *);
bool isEmpty(ArrayList*);
int getNumElements(ArrayList*);
int getConSize(ArrayList*);
#endif
_______________________________________________________________________
#include<stdio.h>
#include<stdlib.h>
#include"Consts.h"
#include<memory.h>
#include "ArrayList.h"
#define CAPACITY 5
#define EXTRA 100

ArrayList *createArrayList(int elementSize) {
    ArrayList *arrayList = malloc(sizeof(ArrayList));
    arrayList->elementSize = elementSize;
    arrayList->conSize = CAPACITY;
    arrayList->numElements = 0;
    arrayList->con = malloc(elementSize * CAPACITY);
    return arrayList;
}

void freeArrayList(ArrayList * arrayList) {
    if (arrayList == NULL) {
        return;
    }else {
        free(arrayList->con);
        free(arrayList);
    }
}
int add(ArrayList *list, void *input) {
    if (list != NULL && input != NULL) {

        if (list->numElements >= list->conSize) {
            resize(list);
            printf("resized\n");
        }
        list->con = input;
        memcpy((char*)list->con + (list->numElements*list->elementSize), input, list->elementSize);
        list->numElements++;
        return 1;
    }
    return -1;
}

void resize(ArrayList *list) {
    void *temp = realloc(list->con, (list->conSize + EXTRA) * list->elementSize);
    if (temp != NULL) {
        list->conSize += 100;
        list->con = temp;
    }
}


int remove(ArrayList * list, int i) {
    if (list != NULL) {
        //find index of value to remove
        int elementSize = list->elementSize;
        int lenToMove = elementSize * (list->numElements - (i + 1));
        memmove((char *)list->con + i*elementSize, (i+1)*elementSize, lenToMove);
        list->numElements--;
        return 1;
    }
    return -1;
}

void printList(ArrayList *list) {
    if (list != NULL) {
        char *p = list->con;
        for (int i = 0; i < list->numElements; i++) {
            void* val = getAtIndex(list, i);
            printf("%d \n", val);
        }
    }
}

void *getAtIndex(ArrayList *listptr, int index) {
    return (char*)listptr->con + index * (listptr->elementSize);
}

int getNumElements(ArrayList * list) {
        return list->numElements;
}

int getConSize(ArrayList * list) {
    return list->conSize;
}

bool isEmpty(ArrayList * list) {
    return list->numElements == 0;
}

___________________________________________________

#include<stdio.h>
#include<stdlib.h>
#include"ArrayList.h"
#include "Consts.h"
#pragma warning(disable : 4996)
int main() {

    ArrayList * list = createArrayList(sizeof(int));
    int x = 5;
    add(list, &x);
    printf("%d",(int *)getAtIndex(list, 0));



    freeArrayList(list);
    system("pause");
    return 0;
}
Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
  • 4
    Does this answer your question? [Making a generic ArrayLIst in C](https://stackoverflow.com/questions/61534745/making-a-generic-arraylist-in-c) – user12986714 May 01 '20 at 02:24
  • You might want to accept the answer to your previous question before asking a new one and you probably should be more specific about what pieces are not working. – idz May 01 '20 at 02:26
  • Maybe try not to create an exact copy of your existing question? You can draw attention to it by editing it. It will bump up on active questions tab. – user12986714 May 01 '20 at 02:27

1 Answers1

1

Pay attention to the comments above. I know you're new to the site, but it's worth while getting to know how it works.

Anyway the problem is in this line of code:

printf("%d",(int *)getAtIndex(list, 0));

getAtIndex() returns a pointer so you need to dereference it to get the value. It should be:

printf("%d",*(int *)getAtIndex(list, 0));
idz
  • 12,825
  • 1
  • 29
  • 40
  • Thank You @idz, i apologize for the messiness. Any thoughts about the printList function – user13443636 May 01 '20 at 03:05
  • You probably won't be able to write a generic version of this yet (you would need to take a function pointer argument to print the unknown element). But your current printList version just needs: `printf("%d \n", *(int*)val);` – idz May 01 '20 at 04:22
  • So you accept answers by clicking the green check mark by an answer. This means that people don't waste time looking at your questions that already have accepted answers and also gives reputation to the folks, like myself, that take time to answer. More info here: https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – idz May 01 '20 at 04:24