通过C中的realloc()函数对resize数组进行堆损坏

时间:2017-03-15 22:09:55

标签: c arrays pointers realloc heap-corruption

我有一个问题,将C []数组从6增加到10我通过realloc()面临堆损坏问题。我有以下代码Big M metod:

#include <stdio.h>
#include <stdlib.h>

int * mnInit(FILE * );
double * * ReadA(FILE * , int, int);
double * ReadVector(FILE * , int);
int * condtxt(FILE *, int );
void Artif_counter(int* , int *, int);
void reallocationA(double**, int* , int , int);
void increaseA(int*, double**, int, int, int);
void increaseC(double *, int);
int main(int argc, char * argv[]) {

  FILE * file1 = fopen("C.txt", "r");
  FILE * file4 = fopen("A.txt", "r");
  FILE * file5 = fopen("Agetmn.txt", "r");
  FILE * file6 = fopen("cond.txt", "r");

  int * ptr_mn;
  ptr_mn = mnInit(file5);
  int n = * (ptr_mn);
  int m = * (ptr_mn + 1);

  double * * A;
  A = ReadA(file4, n, m);
  double * C;
  C = ReadVector(file1, n);
  int * cond;
  cond = condtxt(file6, m);
  for(int i = 0; i < m; i++){
  }
//--------------------------------------------------
int BAcounter = 0;
Artif_counter(cond, &BAcounter, m);
printf("\n  Basys and Artifical variable = %d", BAcounter);
reallocationA(A, &n, m, BAcounter);
increaseA(cond, A, n, m, BAcounter);

这个功能不起作用

increaseC(C, n); 

当我尝试打印数组时:A [] []打印正确,而C [] array 打印未知数字,并且在程序Mmetod.exe关闭后出现问题:堆已损坏(参数:0x00007FFCA0C1F6B0)。

 // count of basys and artif
 //------------------------------------------------After Adding a new columns:
 printf("\n A[][] ARRAY:\n");
 for (int i = 0; i < m; i++) {
 printf("%d ", i);
 for (int j = 0; j < n; j++) {
  printf(" %.3f ", A[i][j]);
 }
  printf("\n");
 }

 printf("\n\tVECTOR C[]:\n");
 for (int i = 0; i < n; i++) {
 printf("%lf ", C[i]);
 }
 fclose(file1);
 fclose(file4);
 fclose(file5);
 fclose(file6);
 free(C);
 for (int i = 0; i < m; i++) {
 free(A[i]);
 }
 free(A);
 printf("\n\n");
 system("pause");
 return 0;
}

int * mnInit(FILE * file) {
int c;
int digit = 0;
int column = 0;
int * mnArray = malloc(2 * sizeof( * mnArray));
if (file == NULL) perror("Warning!");
else {
while (!feof(file)) {
  c = fgetc(file);
  if (c == ';') {
    column++;
  }
  if (c == ' ') {
    digit++;
  }
 }
}
* (mnArray) = (digit / column) + 1; * (mnArray + 1) = column;
return mnArray;
}

double * * ReadA(FILE * file, int n, int m) {
  double * * A = malloc(m * sizeof( * A));
  for (int i = 0; i < m; i++) {
  A[i] = malloc(n * sizeof( * A[i]));
  }

  for (int i = 0; i < m; i++) {
  for (int j = 0; j < n; j++) {
  //fscanf(file ,"%lf ", &(*(*(A + i) + j)));
  fscanf(file, "%lf ", & A[i][j]);
      }
  fscanf(file, "\n");
    }
  return A;
  }
 double * ReadVector(FILE * file, int m) { // ++++
      double * Vector = malloc(m * sizeof( * Vector));
      for (int i = 0; i < m; i++) {
        fscanf(file, "%lf ", Vector + i);
      }
      return Vector;
     }

   int* condtxt(FILE * file, int m){
      int * condA = malloc(m * sizeof(*condA));
    for(int i = 0; i < m; i++){
    fscanf(file, "%d", &condA[i]);
   }
    return condA;
  }

void Artif_counter(int * cond, int *k, int m){

    for(int i = 0; i < m; i++){
        if(cond[i] == -1){
            (*k)++;
        }
        if(cond[i] == 0){
        (*k)++;
        }
        if(cond[i] == 1){
        *k = (*k) + 2;
        }
    }
 }
void reallocationA(double** A, int* n, int m, int k){
    double * tmp = NULL;
    for(int i = 0; i < m; i++){
        tmp = realloc(A[i], ((*n) + k)*sizeof(*A[i]));
        if(tmp){
            A[i] = tmp;
            tmp = NULL;
        } else{
            printf("Error! Memory isn't reallocated'");
            tmp = NULL;
        }
    }
    (*n) = (*n) + k;
 }
void increaseA(int* cond, double** A, int n, int m, int k){
    int presentcol = n-k;
    for(int i = 0; i < m; i++){
        if(cond[i] == -1){
            for(int j = 0; j < m; j++){
                if(j == i){
                    A[j][presentcol] = 1;
                } else {
                    A[j][presentcol] = 0;
                }
            }
            presentcol++;
        }
        if(cond[i] == 0){
            for(int j = 0; j < m; j++){
                if(j == i){
                    A[j][presentcol] = 1;
                } else {
                    A[j][presentcol] = 0;
                }
        }
        presentcol++;
    }
        if(cond[i] == 1){
            for(int j = 0; j < m; j++){
                if(j == i){
                    A[j][presentcol] = 1;
                    A[j][presentcol + 1] = -1;
                    } else {
                    A[j][presentcol] = 0;
                    A[j][presentcol + 1] = 0;
                    }
                }
            presentcol = presentcol + 2;
            }
        }
    }

当我想通过这个函数在一个简单的代码中增加一个数组时,它已经完成了,我不明白为什么( GNU调试器重新启动:0警告:检测到严重错误c0000374

    void increaseC(double * C, int n){
     double * tmp;
     tmp =  realloc(C, (n)*sizeof(*C)); // found out that realloc function caused an error
     if(!tmp){
     printf("Error");
     } else{
      C = tmp;
     }
      tmp = NULL;
    }

0 个答案:

没有答案