c - 函数工作 ramdonly、覆盖内存、双重释放、损坏 (!prev)
问题描述
我学习 C 编程,我认为我做得很好,但这我已经尝试了几个小时,但我不明白我做错了什么。我做了一个打印数组的函数,它工作正常,但只是第一次,后来打印奇怪的字符 o 不起作用,用 gdb 看到它工作正常,但是当我第二次调用 printArray 函数时 integerToString 没有第二次工作。老实说,我不知道如何解决它,我请求一些帮助;-; 我问你评论什么
以下代码是一个最小的可重现示例,但我认为问题只是在 seeArray 函数和 integerToString 函数中
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//Dependecy
int randomInRange(int lower, int upper){
return (random() % (upper - lower + 1)) + lower;
}
//Dependecy
int countDigits(int num, int * numSize){
*numSize = 0;
do{
(* numSize)++;
num /= 10;
}while(num != 0);
return 0;
}
//Here is where things gets broke!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int integerToString(int num, char** strNum, int* strNumSize){
countDigits(num, strNumSize);
*strNum = (char *) malloc(sizeof(char) * (*strNumSize));
if(*strNum == 0x0){
fprintf(stderr, "No enough memory for convert interger to string");
exit(EXIT_FAILURE);
}
for(int i = (*strNumSize-1); i > -1; i--){
*( (*strNum) + i ) = num%10 + '0';
num /= 10;
}
return 0;
}
//Dependecy
int initArray(int** array, int size){
if(size<1){
fprintf(stderr, "The array\'s size most be minimun one");
exit(EXIT_FAILURE);
}
*array = (int*) malloc(sizeof(int)*size);
if(*array == NULL){
fprintf(stderr, "Couldn\'t reserve memory for array");
exit(EXIT_FAILURE);
}
for(int i = 0; i < size; i++) *((*array)+i) == 0;
return 0;
}
//Here is where things gets broke!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int seeArray(int* array, int size, char** arrayPhotography, int* arrayPhotographySize){
int dataSize = 0;
char* data = 0x0;
for(int i = 0; i < size; i++){
integerToString(*(array+i), &data, &dataSize);
*arrayPhotographySize += sizeof(char) * ( 2 + dataSize );
if(*arrayPhotography == 0x0){
*arrayPhotography = (char *) malloc(*arrayPhotographySize);
}else{
*arrayPhotography = (char *) realloc(*arrayPhotography,*arrayPhotographySize);
}
if(*arrayPhotography == 0x0){
fprintf(stderr,"Not enoug memory for array\'s photography");
exit(EXIT_FAILURE);
}
strcat(*arrayPhotography, "[");
strcat(*arrayPhotography, data);
strcat(*arrayPhotography, "]");
free(data);
data = 0x0;
}
free(data);
return 0;
}
//Dependecy
int printArray(int* array, int size){
int arrayPhotographySize = 0;
char* arrayPhotography = 0x0;
if(seeArray(array, size, &arrayPhotography, &arrayPhotographySize)){
fprintf(stderr, "Fuction printArray");
exit(EXIT_FAILURE);
}
printf("Array:%s\n", arrayPhotography);
free(arrayPhotography);
return 0;
}
//Here is where things gets broke!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int main(int argc, char * argv[]){
srand(time(NULL));
int arraySize = randomInRange(1, 15);
int* array = 0x0;
initArray(&array, arraySize);
for (int i = 0; i < 10; i++){
printArray(array, arraySize);
}
free(array);
}
这是如何随机工作的图像(我认为是这样)
解决方案
- 看来,在您的
initArray
函数中,在最终的 for 循环中,您编写==
而不是对=
数组进行零填充。附带说明一下,标头中有一个函数calloc
,<stdlib.h>
它会自动为您动态分配的数组填零,这对于避免像这样的简单错误很方便。 - 在您的
integerToString
函数中,您只有malloc
足够的空间来保存字符串中的位数。然而,这是容易出错的代码,因为 C 中的大多数字符串操作(包括strcat
)都要求字符串以空字符终止。为一个额外的字符分配空间,然后确保最后一个字符为 0。
推荐阅读
- typescript - 使用 TypeScript 时如何增强 Hapi.js 服务器方法?
- c++ - 如何在 ROS Qt Creator 插件中设置可执行文件?
- mysql - MySQL - 计算表中重置事件以来的累积
- python - 使用 python3 查找空闲的 tcp 端口
- mysql - 如何打开和关闭数据库连接
- amcharts - 在amcharts4上将带有千位分隔符的数字格式化为空字符串
- jquery - 加载addressValue jQuery的特定div
- python - 安装 polyglot 时出现 pip 异常:命令“python setup.py egg_info”失败,错误代码为 1
- postgresql - 延迟作业无法连接到 PG
- apache-spark - Spark ML:平滑输入数据