简体   繁体   English

程序的执行停止-C(内存分配)

[英]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. 我正在尝试为USACO的回文广场编写解决方案。经过大量检查,尽管我发现了很多错误,但仍然找不到为什么我的程序仍然要停止的原因。 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 释放所有对baseB的呼叫
  3. lst is not a pointer anymore lst不再是指针

the code to main() is now: 现在, main()的代码为:

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. 一个问题是, baseB返回一个malloc指针,该指针随后被传递到函数中,并且从不释放。 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. 您修改“ num”指针的本地副本。 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). 调试器比SO更好(至少在这种情况下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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