首页 > 解决方案 > 函数工作 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);
}

这是如何随机工作的图像(我认为是这样)

在此处输入图像描述

标签: cmallocfreerealloc

解决方案


  1. 看来,在您的initArray函数中,在最终的 for 循环中,您编写==而不是对=数组进行零填充。附带说明一下,标头中有一个函数calloc<stdlib.h>它会自动为您动态分配的数组填零,这对于避免像这样的简单错误很方便。
  2. 在您的integerToString函数中,您只有malloc足够的空间来保存字符串中的位数。然而,这是容易出错的代码,因为 C 中的大多数字符串操作(包括strcat)都要求字符串以空字符终止。为一个额外的字符分配空间,然后确保最后一个字符为 0。

推荐阅读