【问题标题】:malloc function is not working properly in C program. the program crashesmalloc 函数在 C 程序中无法正常工作。程序崩溃
【发布时间】:2012-07-08 17:43:32
【问题描述】:

我是 C 初学者,这是我第一个使用 malloc() 函数的程序。我认为使用此功能可能存在一些问题。我想使用一个数组(循环长度),其中将放置一个数字范围(用户输入)的解决方案。所以数组大小取决于用户所以我使用了 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;
}

【问题讨论】:

  • 您应该发布带​​有问题的崩溃输出。它将帮助您更快地获得更好的答案。

标签: c malloc


【解决方案1】:

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

(您的错误处理可能会有所改进;您打印"process aborted",然后继续处理。)

【讨论】:

    【解决方案2】:

    你在freed 之后使用cyclelength 并将其设置为NULL

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

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

    【讨论】:

      【解决方案3】:

      您在释放循环长度后使用它,因此您的程序崩溃了。
      试试这个:

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

      【讨论】:

      • 好吧..谢谢大家...我有关于malloc函数的问题..这段代码中还有另一个关于声明计数变量的问题..这是一个愚蠢的错误。我很抱歉。
      • 你也应该改变这一行 int *cyclelength=malloc(5000*sizeof(int)); to int *cyclelength=(int *)malloc(5000*sizeof(int));
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多