0

私はCの初心者で、これはmalloc()関数を使用する最初のプログラムです。この関数の使用には問題があるのではないかと思います。ある範囲の数値(ユーザー入力)の解が得られる配列(cyclelength)を使用したかったのです。配列のサイズはユーザーによって異なるため、malloc()を使用しました。しかし、プログラムはクラッシュしています。これが私のコードです:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x,y,num,count,p,k;
    for(;;){
        printf("enter first integer. must be between 1 and 100000\n");
        scanf("%d", &x);
        printf("enter second integer. must be between 1 and 100000. must not equal the first integer.\n");
        scanf("%d", &y);
        if(x>=1 && x<100000 && y>=1 && y<100000 && x!=y){
            break;
        }
        else{
            printf("try the whole process again\n");
        }
    }
    if (x<y){
        int j;
        j=y;
        y=x;
        x=j;
    } //making x always greater than y
    int *cyclelength=malloc(5000*sizeof(int));
    if (NULL==cyclelength){
        printf("process aborted");
    }
    else{
        /*solution part for the range of number. and solution for each number  put into cyclelength.*/
        num=y;
        while(num<=x){
            p=1;
            k=num;
            while(k!=1){
                if(k%2==0)
                    k=k/2;
                else
                    k=3*k+1;
                p+=1;
                }
            count=0;
            cyclelength[count]=p;
            num+=1;
            count+=1;
        }
        free(cyclelength);
        cyclelength=NULL;
    }
    int c=0;
    int max=cyclelength[c];
    for(;c<x-y;c+=1){
        if(max<cyclelength[c+1]){
            max=cyclelength[c+1];
        }
    }
    printf("%d,%d,%d",x,y,max);
    return 0;
}
4

3 に答える 3

4

あなたはそれが指しているメモリ(というよりは、それが指しているメモリ)を呼び出しアクセスしていますfree(cyclelength)

(そして、エラー処理はある程度の改善につながる可能性があります。印刷"process aborted"しますが、処理を続行します。)

于 2012-07-08T17:46:38.630 に答える
4

あなたはそれをdして設定したcyclelength後に使用しています:freeNULL

        free(cyclelength);
        cyclelength=NULL;
    }
    int c=0;
    int max=cyclelength[c];
    for(;c<x-y;c+=1){
        if(max<cyclelength[c+1]){
            max=cyclelength[c+1];
        }

これは未定義の動作であり、クラッシュする可能性があります。

于 2012-07-08T17:47:05.240 に答える
0

割り当てを解除した後にcyclelengthを使用しているため、プログラムがクラッシュします。
これを試して :

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x,y,num,count,p,k;
    for(;;){
        printf("enter first integer. must be between 1 and 100000\n");
        scanf("%d", &x);
        printf("enter second integer. must be between 1 and 100000. must not equal the first integer.\n");
        scanf("%d", &y);
        if(x>=1 && x<100000 && y>=1 && y<100000 && x!=y){
            break;
        }
        else{
            printf("try the whole process again\n");
        }
    }
    if (x<y){
        int j;
        j=y;
        y=x;
        x=j;
    } //making x always greater than y
    int *cyclelength=(int *)malloc(5000*sizeof(int));
    if (NULL==cyclelength){
        printf("process aborted");
    }
    else{
        /*solution part for the range of number. and solution for each number  put into cyclelength.*/
        num=y;
        while(num<=x){
            p=1;
            k=num;
            while(k!=1){
                if(k%2==0)
                    k=k/2;
                else
                    k=3*k+1;
                p+=1;
                }
            count=0;
            cyclelength[count]=p;
            num+=1;
            count+=1;
        }        
        // don't assign null to cyclelength
        //cyclelength=NULL;
    }
    int c=0;
    int max=cyclelength[c];
    for(;c<x-y;c+=1){
        if(max<cyclelength[c+1]){
            max=cyclelength[c+1];
        }
    }
    printf("%d,%d,%d",x,y,max);
    // free here
    free(cyclelength);
    return 0;
}
于 2012-07-08T17:50:54.337 に答える