HI There,

I've written a fifo implementation in C featuring two functions, one is push() and one is pop() to queue up strings i.e. character arrays. The push function would just push the new element onto the end of an array whereas the pop function would always get the element at [0] and shift the whole content one element "down".
My problem now is that i'm not 100% sure about how i can pop elements off without having a memory leak. My two functions look like this:

Code:
int push(char ***list, char *str, int curlen){

  char **temp;
  int i=0;

  temp = realloc((*list),(curlen+1)*sizeof(*list));
  if (temp==NULL){
    printf("push(): Error reallocating memory for msglist\n");
    for (i=curlen;i>=0;i--) {
      free((*list)[i]);
      QLen--;
    }
    free(list);
    return -1;
  }
  (*list)=temp;
  (*list)[curlen]=malloc (strlen(str)+1);
  strcpy((*list)[curlen],str);

  return ++curlen;
}
//-------------------------------------------------------
int pop (char **list, char *outstr, int curlen)
{
  int j=0;
  char **temp=NULL;
  if (curlen==0) {
    printf("pop(): No element left in list\n");
    outstr="";
    return 0;
  }

  strcpy(outstr, list[0]);
//  temp = realloc(temp,(curlen-1)*sizeof(*list));
  for (j=1; j<curlen; j++){
    temp = realloc(list,(curlen)*sizeof(*list));
    if (temp==NULL){
      printf("pop(): Error reallocating temp memory for msglist\n");
      for (curlen=curlen;curlen>=0;curlen--)
        free(temp[curlen]);
      free(temp);
      return -1;
    }
    temp[j-1]=malloc (strlen(list[j])+1);
    temp[j-1] = list[j];
  }
  //free(list[curlen-1]);
  list=temp;
  /*for(j=curlen-1;j>=0;j++)
    free(temp[j]);*/
  return curlen-1;
}