c - 调试初学者 C 崩溃,寻找发现错误的替代逻辑

标签 c string debugging loops null

我正在研究一个(大部分)工作的程序,然后......我遇到了一个错误,导致系统在终点线完全崩溃。在通过打印筛选查明泄漏后,我有理由相信我已经在它的隐藏位置找到了 gremlin,但我想不出任何杀虫剂解决方案!

你们对解决这个难题有什么想法或建议吗?我认为一双新鲜的眼睛会创造奇迹!非常感谢您抽出宝贵时间,我很确定最终这一切真的很简单! >_<

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


程序+脚本信息:


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

这是我发现的异常情况

  • Main_Size 总是最终结果 1 高于应该的值。
  • 错误可能源于我创建的 loop() 函数。通常当用户输入一个空字符串时,这应该是它的结尾。但是该程序似乎计算该空字符串并将其发送到程序集中,最终将 NULL 值放入主 int 数组中。我几乎 100% 肯定这就是我的错误所在。我已经尝试了多种不同的方法来检测空字符串并避免将其发送到程序集的其余部分,但到目前为止没有运气:(
  • 我使用的积极的打印调试似乎在打印单个字符串的最后一轮“破坏编队”。 还有一个换行符,我不知道它是怎么到那里的。
  • 使用 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);
}

最佳答案

线

realloc(Main_Array, sizeof(int));

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

您可以通过将 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 */
}

关于c - 调试初学者 C 崩溃,寻找发现错误的替代逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500481/

相关文章:

.net - Visual Studio 的线程窗口如何识别 "Main Thread'

c++ - 将后台进程发送到前台

c - 如何在 C 结构中使用二维数组?

javascript - 如何创建一个将任何给定字符串与第二个参数连接起来的函数?

python - Python 字符串 `\x` 中的前导 `\xaa` 是什么意思

string - 如何测试 Robot Framework 中的字符串变量是否为空?

javascript - 无法在 .js 文件中命中断点

c++ - 为什么谷歌测试会出现段错误?

c++ - 链接 C 和 C++ 目标文件

c - 我如何解析这个 json 以获取字符串并将它们存储在变量中?