调试初学者C崩溃,寻找发现错误的备用逻辑

时间:2013-05-11 18:36:40

标签: c string debugging loops null

我正在研究一个正在运行的程序(大多数情况下)......我遇到了一个错误,导致终点线系统完全崩溃。在进行了印刷筛选以确定泄漏之后,我有理由相信我已经发现了它的隐藏点,但我无法想到任何一种喷雾解决方案!< / p>

您是否有任何关于解决这个难题的想法或建议?我觉得一双新鲜的眼睛会创造奇迹!我非常感谢你的时间,我确信这一切真的很简单! &GT; _&LT;

感谢您提出任何意见或建议!


有关程序+脚本的信息


  1. 取任意数量的用户输入(仅限数字) 字符串
    • 当用户输入空字符串时,程序将检测到该字符串作为用户输入的结束,并继续进行包装。
  2. 标记化并将每个标记转换为整数。
  3. 将整合添加到动态分配的数据库中。

  4. 以下是我发现的异常

    • Main_Size 总是最终结果1 应该
    • 错误可能源于我创建的loop()函数。通常,当用户输入空字符串时,应该是它的结尾。但程序似乎计算出空字符串并将其发送到程序集中,其中NULL值最终被置于主int数组中。我的错误几乎是 100%肯定的。我已经尝试了多种不同的方法来检测空字符串,并避免将其发送到汇编的其余部分,但到目前为止没有运气:(
    • 我使用的积极的打印调试似乎&#34;打破形成&#34;在最后一轮打印一个字符串。 还有一个额外的换行符,我不知道它是如何到达那里的。
    • 使用scanf提示信号通知用户输入字符串的结尾会产生良好的结果,但是在结束之前输入多个字符串的那一刻,程序会变得混乱。

     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_SIZE 12
    #define MIN_SIZE 2
    
    int loop(int* Main_Array, char str[], int Main_Size);
    int* Create_Main_Array();
    int Input_String(char str[]);
    int Token_Atoi(char str[], int numElements, int* Main_Array, int Main_Size);
    int Dynamic_Fitting(int* Main_Array , int Incomming_int , int Main_Size);
    void Free_All(int* Main_Array);
    
    //////////////////// 
    /////////////////////////////////////////////////
    int main()
    {
       char str[MAX_SIZE];
       int* Main_Array = Create_Main_Array(), Main_Size = 0;
       //Main_Size = The number of elements currently in the dynamic memory. 
       //This number should increase by 1 for every new int you add into the 
       //array, starting from zero, as in Main_Array[0] = ####
    
       Main_Size = loop(Main_Array, str, Main_Size);
    
       printf("\n\nMain_Size final size is: %i\n", Main_Size);
    
       for(int i=0; i<Main_Size; i++)
               printf("Check: %i \n", Main_Array[i]);
       Free_All(Main_Array);
    
       system("PAUSE");
       return 0;
    }
    
    /////////////////////////////////////////////////
    //Sets up Dynamic Space. It will be realloced in the future. 
    /////////////////////////////////////////////////
    int* Create_Main_Array()
    {
         return (int*)malloc(MAX_SIZE*sizeof(int));
    }
    
    /////////////////////////////////////////////////
    //Calls up the user to input a string. 
    //Loops the entire process so long as returned string is larger then 0.
    //Returns total Element size of Main, after it's been modified in the program.
    /////////////////////////////////////////////////
    int loop(int* Main_Array, char str[], int Main_Size)
    {
       int numElements;
       while(numElements>0)
       {
          numElements = Input_String(str);
          //for some reason, at the very end of the loop, it will tag on another '\0' 
          //into the Main_Array, which causes a crash. Likely the setup at line 52.
          Main_Size = Token_Atoi(str, numElements, Main_Array, Main_Size);
       }
       return Main_Size;   
    }
    
    /////////////////////////////////////////////////
    //Enters strings, and returns size of the strings.
    //Will not count Line breaks as a character. 
    //Going under or over a limit will trigger a reroute. 
    /////////////////////////////////////////////////
    int Input_String(char str[])
    {
       printf("\nPlease input a string of numbers.\n");
       printf("Tap enter again once finnished: \n\n");
       int i=0;
       while ((str[i] = getchar()) != '\n')
          i++;
       str[i+1]='\0';
       return i;
    
       if (i>MAX_SIZE-1 || i<MIN_SIZE)
          {
             printf("Your sumbition dosn't fit the size criteria.\n");
             printf("Please reenter:\n\n");
             Input_String(str);
          }
    }
    
    /////////////////////////////////////////////////
    //Tolkenizes string, Atoi each token into cute little ints.
    //Each int will be sent to the Dynamic_Fitting to be assimilated into Main_Array
    //Main_Size is passed into this function just to be used as parameters for the fitting.
    /////////////////////////////////////////////////
    int Token_Atoi(char str[], int numElements, int* Main_Array, int Main_Size)
    {
       char* temp = strtok(str, " -");
    
       int i=0;
       while (temp != NULL)
       {         
             printf("String tokenize check: %s\n", temp);
             Main_Size = Dynamic_Fitting(Main_Array, atoi(temp), Main_Size);
             //Main size should be upgraded with each loop of the above line. 
             temp = strtok(NULL, " -");
             i++;
       }
       return Main_Size;     
    }
    
    /////////////////////////////////////////////////
    //Will first increase the size of the dynamically allocated array of ints by 1 int
    //Then, it will add the incomming int into the re-sized dynamic space. 
    //Main size serves as a bookmark to show where on the array the new realloc'd spot should be. 
    /////////////////////////////////////////////////
    int Dynamic_Fitting(int* Main_Array , int Incomming_int , int Main_Size)
    {
        realloc(Main_Array, sizeof(int));
        Main_Array[Main_Size]= Incomming_int;
        printf("Dynamic fitting check: %i put into Main_Array[%i]\n\n", Incomming_int, Main_Size);
        return Main_Size+1;
    }   
    
    /////////////////////////////////////////////////
    //Close shop 
    /////////////////////////////////////////////////
    void Free_All(int* Main_Array)
    {
       free(Main_Array);
    }
    

2 个答案:

答案 0 :(得分:4)

该行

realloc(Main_Array, sizeof(int));

错了。 realloc返回一个可能指向新内存的指针,因此当您在下一行取消引用Main_Array时,您可能正在访问释放的内存。此外,您需要传递更新的内存缓冲区的完整大小,而不仅仅是delta。

您可以通过将Dynamic_Fitting更改为

来解决问题
int Dynamic_Fitting(int** Main_Array , int Incomming_int , int Main_Size)
{
    int* temp = realloc(Main_Array, (Main_Size +1) * sizeof(int));
    if (temp == NULL) {
        return -1; /* caller must handle oom error */
    }
    *Main_Array = temp;
    (*Main_Array)[Main_Size] = Incomming_int;
    printf("Dynamic fitting check: %i put into Main_Array[%i]\n\n", Incomming_int, Main_Size);
    return Main_Size+1;
}

并将其称为

Main_Size = Dynamic_Fitting(&Main_Array, ....);
if (Main_Size == -1) {
    /* out of memory.  cleanup and exit program */
}

答案 1 :(得分:1)

我想这是问题之一。 你已经分享了一个非常沼泽的代码,分析一切都是一项任务......看看下面的问题是否解决了你的问题..

int Input_String(char str[])
{
   printf("\nPlease input a string of numbers.\n");
   printf("Tap enter again once finnished: \n\n");
   int i=0;
   while ((str[i] = getchar()) != '\n') //  HERE if user enters more than array Size it will fail... with exception
      i++;
   str[i+1]='\0';
   return i; /// THIS is wrong.. following if will never execute..

   if (i>MAX_SIZE-1 || i<MIN_SIZE)
      {
         printf("Your sumbition dosn't fit the size criteria.\n");
         printf("Please reenter:\n\n");
         Input_String(str);
      }
}

我将在下面建议

int Input_String(char str[])
    {
       printf("\nPlease input a string of numbers.\n");
       printf("Tap enter again once finnished: \n\n");
       int i=0;
       while ((str[i] = getchar()) != '\n') {
          i++;
          if (i>MAX_SIZE-1 || i<MIN_SIZE)
          {
             printf("Your sumbition dosn't fit the size criteria.\n");
             printf("Please reenter:\n\n");
             Input_String(str);
             break;
          }

      }
       str[i+1]='\0';
       return i; 
         }