I am trying to write a solution for the USACO's [Palindromic Squares](http://cerberus.delos.com:790/usacoprob2?a=ZZFfoPygSwa&S=palsquare)
After a lot of checks, although I found a lot of bugs, I still can't find why my program is still consisting on stopping.
I believe it is a kind of a memory management problem, but I don't get why or how it is so. So, here is the code:

```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char ch(int x){
    if (x < 0 || x > 19) return 0;
    return "0123456789ABCDEFGHIJ"[x];
}

void append( int x, char* num){
    num=realloc(num,sizeof(char)* strlen(num)+2);
    num[strlen(num)+1] = '\0';
    num[strlen(num)] = ch(x);

}

char * baseB(int x, int base){
    int mult=1,lim=1,i;
    char *num;
    num = malloc(sizeof(char));
    num[0] = '\0';
    while(x/(mult*base)){
        mult*=base;
        lim++;
    }
    for(i=0;i<lim;i++){
        append(x/mult,num);
        x %= mult;
        mult/=base;
    }
    return num;
}

int is_pal( char* num ){
    int i;

    for(i=0;i<strlen(num)/2;i++){
        if ( num[i] != num[strlen(num)-1-i] )
            return 0;
    }
    return 1;
}
int main(){

    int x, size=0, y, base;
    int *lst;
    FILE *fp;
    lst=malloc(sizeof(int));
    fp= fopen("palsquare.in","r");
    fscanf(fp,"%d", &base);
    fclose(fp);

    for(x=1;x<301;x++){
        y = x*x;
                    printf(" a0 ");

        printf("%s ", baseB(y,base));
                    printf(" a1 ");

        //printf("%d ", is_pal( baseB(y,base) ) );
                    printf(" a2 ");

        if( is_pal( baseB(y,base) ) ){
            printf(" a3\n");
            size++;
            lst=realloc(lst,sizeof(int)*size);
            lst[size-1]=x;
        }
    }

    fp=fopen("palsquare.out","w");
    for(x=0;x<size;x++){
        fprintf(fp, "%d %d\n", lst[x], lst[x]*lst[x]);
    }
    fclose(fp);
    return 0;

}

```

The loop to create the result list, seemed to me as the reason of my problem. Any ideas about what happens there, why happens there?

---

edits: 

1. Changed the switch code :)
2. Free'd all the calls to baseB
3. `lst` is not a pointer anymore

the code to `main()` is now:

```
int main(){

    int x, size=0, y, base;
    int lst[300];
    FILE *fp;
    char *tmp = NULL;
    fp= fopen("palsquare.in","r");
    fscanf(fp,"%d", &base);
    fclose(fp);

    for(x=1;x<301;x++){
        y = x*x;
        tmp=baseB(y,base);
        printf("%s ", tmp);
        if( is_pal( tmp ) ){
            size++;
            lst[size-1]=x;
        }
        free(tmp);
        tmp=NULL;
    }

    fp=fopen("palsquare.out","w");
    for(x=0;x<size;x++){
        fprintf(fp, "%d %d\n", lst[x], lst[x]*lst[x]);
    }
    fclose(fp);
    return 0;

}

```