Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

What does prog think of my arraylist

Name: Anonymous 2011-11-25 17:03

Please do tell me all my mistakes so i can fix them.
One of the things i worried about is if i were to use this in some other application and the arraylist holds malloc'd data whenever it goes to destroy the arraylist those malloc'd data will still be there unless i explicitly remove/free them so i wasn't sure what to do in the destroy method aside from freeing the objs array itself.


(1) unixs1 $ cat arraylist.h
#ifndef ARRAYLIST_H
#define ARRAYLIST_H

struct arraylist {
  int cap;
  int size;
  void **objs;
  int (*_equals)(void*,void*);
};

struct arraylist *arraylist_create(int,int (*equals)(void *o1,void *o2));
int arraylist_contains(struct arraylist *,void *);
int arraylist_indeof(struct arraylist *,void *);
void arraylist_set(struct arraylist *,int,void *);
void arraylist_add(struct arraylist *,void *);
void *arraylist_remove(struct arraylist *,int);
void *arraylist_get(struct arraylist *,int);
void arraylist_destroy(struct arraylist *);

#endif

(2) unixs1 $ cat arraylist.c
#include <stdio.h>
#include "arraylist.h"

void arraylist_repop(struct arraylist *);


struct arraylist * arraylist_create(int c,int (*equals)(void *o1,void *o2))
{
  struct arraylist *a = malloc(sizeof(struct arraylist));
  if(!a){
    /* error handling if needed */
    printf("Failed to create the arraylist\n");
    exit(1);
  }
  a->cap = c;
  a->size = 0;
  a->_equals = equals;
  a->objs = malloc(sizeof(void *)*a->size);
  if(!a->objs){
    /* Error handling if needed */
    printf("Failed create the arraylist\n");
    exit(1);
  }
  return a;
}

int arraylist_contains(struct arraylist *a,void *e)
{
  return(arraylist_indexof(a,e) > -1 ? 1 : 0);
}

int arraylist_indexof(struct arraylist *a,void *e)
{
  register int i;
  for(i=0;i<a->size;++i)
    if(a->_equals(arraylist_get(a,i),e) != 0){
      return i;
    }
  return -1;
}

void arraylist_set(struct arraylist *a,int in,void *e)
{
  register int i;
  if(a->cap >= a->size+1)
    arraylist_repop(a);
  for(i=a->size;i>=in;--i){
    a->objs[i+1] = a->objs[i];
  }
  a->objs[in] = e;
  a->size++;
}

void arraylist_add(struct arraylist *a,void *e)
{
  if(a->cap >= a->size+1)
    arraylist_repop(a);
  a->objs[a->size] = e;
  a->size++;
}

void * arraylist_remove(struct arraylist *a,int in){
  register int i;
  void *e = a->objs[in]; /* error if in >= size || < 0 */
  for(i=in;i<a->size;++i){
    a->objs[i] = a->objs[i+1];
  }
  a->size--;
  return e;
}

void * arraylist_get(struct arraylist *a,int in)
{
  return a->objs[in]; /* will error if in >= size || < 0 */
}

void arraylist_repop(struct arraylist *a)
{
  register int i;
  a->cap = a->cap * 2;
  void **ob = a->objs;
  a->objs = malloc(sizeof(void *)*a->cap);
  if(!a->objs){
    /* Error handle */
    printf("Failed to repop\n");
    exit(1);
  }

  for(i=0;i<a->size;++i)
    a->objs[i] = ob[i];
  free(ob);
}

void arraylist_destroy(struct arraylist *a)
{
  free(a->objs);
}


int equals(void *o,void *o2)
{
  char *s1 = (char *)o;
  char *s2 = (char *)o2;
  return strcmp(s1,s2) == 0 ? 1 : 0;
}

int main(){
  struct arraylist *test = arraylist_create(1,equals);
  arraylist_add(test,"Hello Faggots");
  printf("AL GET [%s]\n",arraylist_get(test,0));
  arraylist_add(test,"anusify me captain");
  arraylist_add(test,"Holy bible");
  arraylist_set(test,1,"glory to the gods");

  printf("AL SIZE [%d] | CAP [%d]\n",test->size,test->cap);
  while(test->size > 0){
    printf("AL REM [%s]\n",arraylist_remove(test,0));
  }
}
(3) unixs1 $ atest
AL GET [Hello Faggots]
AL SIZE [4] | CAP [16]
AL REM [Hello Faggots]
AL REM [glory to the gods]
AL REM [anusify me captain]
AL REM [Holy bible]

Name: Anonymous 2011-11-25 17:46

>>3
>>5
Thanks I updated the code with your suggestions and removed the register stuff.

May i ask when is it a good time to use register?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List