c - 检测到堆损坏:在正常块之后(#87)
问题描述
我正在尝试做一个从用户那里获取姓名数量的程序,然后它从用户那里获取姓名并将它们保存在字符串数组中。之后,它按 abc 对数组中的名称进行排序,然后打印排序的名称。该程序运行良好,但问题是当我尝试释放我定义的动态内存时。这是代码:
#include <stdio.h>
#include <string.h>
#define STR_LEN 51
void myFgets(char str[], int n);
void sortString(char** arr, int numberOfStrings);
int main(void)
{
int i = 0, numberOfFriends = 0, sizeOfMemory = 0;
char name[STR_LEN] = { 0 };
char** arrOfNames = (char*)malloc(sizeof(int) * sizeOfMemory);
printf("Enter number of friends: ");
scanf("%d", &numberOfFriends);
getchar();
for (i = 0; i < numberOfFriends; i++) // In this loop we save the names into the array.
{
printf("Enter name of friend %d: ", i + 1);
myFgets(name, STR_LEN); // Get the name from the user.
sizeOfMemory += 1;
arrOfNames = (char*)realloc(arrOfNames, sizeof(int) * sizeOfMemory); // Change the size of the memory to more place to pointer from the last time.
arrOfNames[i] = (char*)malloc(sizeof(char) * strlen(name) + 1); // Set dynamic size to the name.
*(arrOfNames[i]) = '\0'; // We remove the string in the currnet name.
strncat(arrOfNames[i], name, strlen(name) + 1); // Then, we save the name of the user into the string.
}
sortString(arrOfNames, numberOfFriends); // We use this function to sort the array.
for (i = 0; i < numberOfFriends; i++)
{
printf("Friend %d: %s\n", i + 1, arrOfNames[i]);
}
for (i = 0; i < numberOfFriends; i++)
{
free(arrOfNames[i]);
}
free(arrOfNames);
getchar();
return 0;
}
/*
Function will perform the fgets command and also remove the newline
that might be at the end of the string - a known issue with fgets.
input: the buffer to read into, the number of chars to read
*/
void myFgets(char str[], int n)
{
fgets(str, n, stdin);
str[strcspn(str, "\n")] = 0;
}
/*In this function we get array of strings and sort the array by abc.
Input: The array and the long.
Output: None*/
void sortString(char** arr, int numberOfStrings)
{
int i = 0, x = 0;
char tmp[STR_LEN] = { 0 };
for (i = 0; i < numberOfStrings; i++) // In this loop we run on all the indexes of the array. From the first string to the last.
{
for (x = i + 1; x < numberOfStrings; x++) // In this loop we run on the next indexes and check if is there smaller string than the currnet.
{
if (strcmp(arr[i], arr[x]) > 0) // If the original string is bigger than the currnet string.
{
strncat(tmp, arr[i], strlen(arr[i])); // Save the original string to temp string.
// Switch between the orginal to the smaller string.
arr[i][0] = '\0';
strncat(arr[i], arr[x], strlen(arr[x]));
arr[x][0] = '\0';
strncat(arr[x], tmp, strlen(tmp));
tmp[0] = '\0';
}
}
}
}
打印名称后,当我想释放名称和数组时,在第一次尝试释放时,出现错误:“HEAP CORRUPTION DETECTED: after normal block(#87)”。顺便说一句,我只有在输入 4 个或更多玩家时才会收到此错误。如果我输入 3 个或更少的玩家,程序可以正常工作。为什么会发生这种情况,我应该怎么做才能解决它?
解决方案
首先删除 and 的返回值的不必要的(部分错误的)强制malloc
转换realloc
。换句话说:替换(char*)malloc(...
为malloc(...
,与 相同realloc
。
然后这里有一个大问题realloc(arrOfNames, sizeof(int) * sizeOfMemory)
::您要分配指针数组而不是int数组,并且指针的大小可能与int的大小相同,也可能不同。你需要sizeof(char**)
或者更确切地说是不太容易出错的sizeof(*arrOfNames)
地方。
此外,这太复杂了(但实际上并没有错):
*(arrOfNames[i]) = '\0';
strncat(arrOfNames[i], name, strlen(name) + 1);
相反,您可以简单地使用它:
strcpy(arrOfNames[i], name);
函数中的相同内容sort
。
保持代码简单。
sort
但实际上你的功能还有更多的问题。您天真地交换字符串的内容(顺便说一句效率低下),但真正的问题是,如果您复制一个较长的字符串,比如复制"Walter"
到一个较短的字符串,比如说"Joe"
,您将写入超出分配内存的末尾"Joe"
.
而不是交换字符串的内容只是交换指针。
我建议你拿一支铅笔和一张纸,画出指针和它们指向的内存。
推荐阅读
- python - 如何使用 Python 在 QGIS 中选择和缩放到图层
- java - 带有 CheckBoxListCell 的 Javafx 列表视图不适用于拖放
- c# - Unity C# 用正确的旋转实例化粒子效果
- bash - 提取第一个和第二个点字符实例之间的子字符串
- solr - 如何查找数据导入失败文档的原因?
- linear-programming - 是否可以在 GLPK 中定义多界优化问题?
- c# - 如何返回字符串“test”并且我正在使用?操作员
- javascript - 如何根据下拉列表中的选择呈现组件?
- laravel - Laravel 5.4 Authentication password reset redirect based on email address
- apache-spark - 从 spark 代码更改单个作业的临时路径