我正在研究一个正在运行的程序(大部分)并且......我遇到了一个错误,导致系统在终点线完全崩溃。在打印筛选以查明泄漏之后,我有理由确定我已经在它的藏身之处找到了小精灵,但我想不出任何错误喷雾解决方案!
你们中有人对解决这个难题有什么想法或建议吗?我认为一双新鲜的眼睛会创造奇迹!我非常感谢您的时间,我很确定这一切都非常简单!>_<
感谢您的任何意见或建议!
有关程序 + 脚本的信息:
- 获取任意数量的用户输入(仅限数字) 字符串
- 当用户输入一个空字符串时,程序将检测到用户输入的结束,并继续包装。
- 标记并将每个标记转换为整数。
- 将整数添加到动态分配的数据库中。
这是我发现的异常情况
- 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);
}