简体   繁体   中英

Program's execution stops - C ( memory allocating )

I am trying to write a solution for the USACO's Palindromic Squares 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;


}

The best place to start is probably freeing all of the memory you've allocated. Granted, I don't know if that will actually fix anything but...

A problem is that baseB returns a malloc'd pointer which is then passed into functions and never freed. Instant leak.

Try something like this:

// At the top of main
char *tmp = NULL;
// All that code until the first baseB
tmp = baseB(y, base);
printf("%s ", tmp); // Or the corresponding is_pal call
free(tmp);
tmp = NULL;

And so on.

num=realloc(num,sizeof(char)* strlen(num)+2);

You modifying local copy of 'num' pointer. Caller function will still have old unmodified address. If address will ever change at least once - you boned.

Since the code is already a mess, easiest version (least modifications) would be:

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

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++){
        num=append(x/mult,num);
        x %= mult;
        mult/=base;
    }
    return num;
}

Aside from that, as comments suggests - debug for once! Debugger is better than SO (at least in this kind of cases).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM