【问题标题】:Problems with memory managment in CC中的内存管理问题
【发布时间】:2018-03-31 21:51:17
【问题描述】:

我有以下 C 函数,这给我带来了很多内存问题,如果我不释放内存它工作正常但是当我调用另一个函数时我得到一个 malloc 内存损坏错误,但我释放它我得到释放无效指针。最后,当我为 fecha 和 fechaVariable 定义新指针时,由于 strtok 的工作很奇怪,我得到了损坏的_size vd prev_size 错误。有人能帮我吗。这是我的代码:

void mensual_precipitacion(struct fila *arregloArchivo, char *precipitacion, int estacion){
    float acumulado = 0.00;
    char *fecha;
    char *fechaVariable;

    int i;
    int boolIncorrecto = 1;
    char *mes;
    char *anio;
    char *lluvia;   
    int boolfecha = 1;

    fecha = malloc(1000*sizeof(char));
    fechaVariable = malloc(1000*sizeof(char));
    lluvia = malloc(1000*sizeof(char));



    for(i=0;i<cantDatos;i++){

        if(arregloArchivo[i].numero != estacion){
            continue;
        }
        boolIncorrecto =0; 
        if(boolfecha){
            strcpy(fecha,arregloArchivo[i].fecha);  
            boolfecha = 0;

            fecha = strtok(fecha,"/");

            mes = strtok(NULL,"/");
            anio = strtok(NULL," ");
        }

        strcpy(fechaVariable,arregloArchivo[i].fecha);

        fechaVariable = strtok(fechaVariable,"/");

        fechaVariable = strtok(NULL, "/");

        if(strcmp(mes,fechaVariable) == 0){
            acumulado += arregloArchivo[i].precipitacion;

        }else{

            sprintf(lluvia,"%s/%s:%f[mm]\n",mes,anio,acumulado);
            strcat(precipitacion,lluvia);
            acumulado = 0.00;
            boolfecha = 1;
            memset(lluvia,'\0',sizeof(lluvia));
        }

    }
    if(boolIncorrecto){
        strcpy(lluvia,"Nro de estacion inexistente");
    }else{
        sprintf(lluvia,"%s/%s:%f[mm]\n",mes,anio,acumulado);
    }
    //
        //printf("%s\n",lluvia );
    strcat(precipitacion,lluvia);

    free(fecha);
    free(fechaVariable);    
    free(lluvia);
}

【问题讨论】:

  • 使用新的strtok_s() 函数比旧的strtok() 更可取,因为它有一个上下文参数来允许同时进行多个标记化。
  • 我更喜欢使用 strncpy() 和 strncat(),它们都需要最大数量的字符来复制或连接,而不是 strcpy() 和 strcat() 函数,以防万一缺少零终止符在从中复制的字符串中。这个想法是通过复制太多来防止缓冲区溢出问题。然后我在最后一个 char 数组元素中放置一个零终止符,以确保新字符串中始终存在一个零字符串终止符。所以char xArray[100]; 我会使用strcpy (xArray, aszSource, 100 - 1); xArray[100 - 1] = 0;
  • memset(lluvia,'\0',sizeof(lluvia)); 也有问题,因为lluvia 的大小是char * 指针的大小,在 32 位机器上为 4 个字节。您需要指定类似1000 * sizeof(char) 的内容。
  • 你永远不需要乘以sizeof(char)。

标签: c memory heap-memory free stack-memory


【解决方案1】:

如果ptr 的值是从malloc() 或realloc() 返回的,您只能调用free(ptr)。您的代码稍后具有:

fecha = strtok(fecha, "/");

所以当你到达函数的末尾时,fecha 不再包含它最初包含的指针:

fecha = malloc(1000 * sizeof(char));

您应该在循环中使用不同的变量,以免丢失原始的fecha 指针。

你有同样的问题:

fechaVariable = strtok(fechaVariable,"/");

所以这里也使用不同的变量。

实际上,完全没有理由对这些变量使用动态分配。只需声明:

char fecha[1000], fechaVariable[1000], lluvia[1000];

然后将不同的变量与strtok 一起使用,因为您不能分配给数组变量。

char *fecha_slash = strtok(fecha, "/");
char *fechaVariable_slash = strtok(fechaVariable, "/");

【讨论】:

  • fechaVariable = strtok(fechaVariable,"/"); 和 fechaVariable = strtok(NULL, "/"); 也一样。
  • @RichardChambers 我创建了例如 *ptr_Fecha = Fecha 然后我释放了 ptr_Fecha,但是当我这样做时,我得到了损坏的大小与 prev_size 错误。
  • @NicoPassaglia 这听起来你可能在数组边界之外写。
  • @Barmar strtok 不允许我将变量定义为 fecha[1000],它表示错误分配给具有数组类型的表达式。也许我写的越界
猜你喜欢
  • 2023-03-24
  • 2012-07-23
  • 2011-05-11
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
  • 2015-12-09
相关资源
最近更新 更多