繁体   English   中英

malloc函数在C程序中无法正常工作。 程序崩溃

[英]malloc function is not working properly in C program. the program crashes

我是C语言的初学者,这是我使用malloc()函数的第一个程序。 我认为使用此功能可能会出现一些问题。我想使用一个array(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;
}

您正在调用free(cyclelength)然后访问它所指向的内存(或者更确切地说,它指向的内存)。

(而且您的错误处理可能会有所改善;您可以打印"process aborted" ,但是继续进行处理。)

free它并将其设置为NULL后,您将使用cyclelength

        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];
        }

这是未定义的行为,可能会崩溃。

释放它之后,您正在使用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;
}

暂无
暂无

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

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