释放C中为非常长的字符串(char*)分配的内存



请,我需要帮助为一个非常长的字符串释放分配的内存。我试着把代码精简成这一小段,它包含了我遇到的问题:

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <limits.h>
#define N 6
#define E 1024
int M[N][N][N], R[N*N][N], C[N*N][N], F[N*N][N];
void init_rcf() {
    int i, j, k, p;
    p = 0;
    for(j=0; j<N; j++) {
        for(k=0; k<N; k++) {
            for(i=0; i<N; i++) 
                R[p][i] = M[i][j][k];
            p++;
        }
    }
    p = 0;
    for(i=0; i<N; i++) {
        for(k=0; k<N; k++) {
            for(j=0; j<N; j++)
                C[p][j] = M[i][j][k];
            p++;
        }
    }
    p = 0;
    for(i=0; i<N; i++) {
        for(j=0; j<N; j++) {
            for(k=0; k<N; k++)
                F[p][k] = M[i][j][k];
            p++;
        }
    }
}
char *bin(int n, int p) {
    int c, d, count;
    char *pointer;
    count = 0;
    pointer = (char*)malloc(p+1);
    for (c = p-1;c >= 0;c--) {
        d = n >> c;
        if (d & 1) 
            *(pointer+count) = 1 + '0';
        else 
            *(pointer+count) = 0 + '0';
        count++;
    }
    *(pointer+count) = '';
    return  pointer;
}
int f0(int n) {
    return ceil(log2(n+1)); 
}
int f1() {
    int sum, max = 0;
    for(int k=0;k<N;k++) {
        for(int j=0;j<N;j++) {
            for(int i=0;i<N-1;i++) {
                sum = M[i][j][k] + M[i+1][j][k]; 
                if (sum > max) 
                    max = sum; 
            }
        }   
    }
    return max;
}
int f2() {
    int sum, max = 0;
    for(int k=0;k<N;k++) {
        for(int i=0;i<N;i++) {
            for(int j=0;j<N-1;j++) {
                sum = M[i][j][k] + M[i][j+1][k]; 
                if (sum > max) 
                    max = sum; 
            }
        }   
    }
    return max;
}
int f3() {
    int sum, max = 0;
    for(int j=0;j<N;j++) {
        for(int i=0;i<N;i++) {
            for(int k=0;k<N-1;k++) {
                sum = M[i][j][k] + M[i][j][k+1]; 
                if (sum > max) 
                    max = sum; 
            }
        }   
    }
    return max;
}
int f4() {
    int m1 = f1(),  m2 = f2(), m3 = f3(); 
    if ((m1 >= m2) && (m1 >= m3)) return m1;
    if ((m1 <= m2) && (m2 >= m3)) return m2;
    if ((m1 <= m3) && (m2 <= m3)) return m3;
}
char *g_fxn() {
    char *g = (char *) malloc(1 + (N*N*N)*3); 
    int k = f0(f4());
    init_rcf();
    strcpy(g,"");            
    for(int i=0; i<N*N; i++) 
        for(int j=0; j<N; j++) 
            strcat(g,bin(R[i][j],k));
    for(int i=0; i<N*N; i++) 
        for(int j=0; j<N; j++) 
            strcat(g,bin(C[i][j],k));
    for(int i=0; i<N*N; i++) 
        for(int j=0; j<N; j++) 
            strcat(g,bin(F[i][j],k));
    return g;
}
void convert2bin(char *file_in) {
    const char *FILENAME_IN = file_in;
    const char FILENAME_OUT[] = "temp.txt";
    char c, d; 
    int k=0;
    FILE *infile;
    FILE *outfile;
    infile = fopen(FILENAME_IN, "rb");
    outfile = fopen(FILENAME_OUT, "w");
    if(infile == NULL){
        fprintf(stderr, "Error: Source file not found.n");
        exit(EXIT_FAILURE);
    }
    while((c = fgetc(infile)) != EOF){
        k++;
        unsigned n = (sizeof(c) * CHAR_BIT) - 1; 
        for (int i=0; i<=n; i++) { 
            int m = (c >> (n-i)) & 1; 
            d = '0'+m;          
            fwrite(&d, 1, sizeof(d), outfile);
        }
    }
    if (k < E) {
        d = '1';
        fwrite(&d, 1, sizeof(d), outfile);
        for (int i=k; i<=E; i++) { 
            d = '0';            
            fwrite(&d, 1, sizeof(d), outfile);
        }
    }
    fclose(infile);
    fclose(outfile);
}
void digest() {
    const char *FILENAME_IN = "temp.txt";
    const char FILENAME_OUT[] = "digest.txt";
    int size = N*N*N; // size of message to construct N*N*N matrix
    char c, msg0[size], *msgf; 
    FILE *infile;
    FILE *outfile;
    infile = fopen(FILENAME_IN, "r");
    outfile = fopen(FILENAME_OUT, "wb");
    if(infile == NULL){
        fprintf(stderr, "Error: Source file "temp.txt" not found.n");
        exit(EXIT_FAILURE);
    }
    int quit = 0;
    while (quit == 0) {
        msgf = (char *) malloc(1 + (size)*3);
        strcpy(msgf, ""); 
        strcpy(msg0, "");
        int p = 0;
        while(((c = fgetc(infile)) != EOF) && (p < size)) {
            msg0[p++] = c;
        }
        if(c == EOF) quit = 1;
        if (p > 0) {
            if (p < size) {
                msg0[p] = '1';
                for(int i=p+1; i<size; i++) 
                    msg0[i] = '0';
            }
            for (int k=0; k<N; k++)
                for (int j=0; j<N; j++)
                    for (int i=0; i<N; i++) {
                        c = msg0[i + N * (j + N * k)];
                        if (c == '0')
                            M[i][j][k] = 0;
                        else 
                            M[i][j][k] = 1;
                    }
            strcpy(msgf, g_fxn());             
            int q = 0;
            while (q<strlen(msgf)) {
                int d;
                char b = 0;
                for (int r=0; r<8; r++) {
                    if (msgf[q++] == '0') 
                        d = 0;
                    else 
                        d = 1;
                    b = ((b<<1) | d);
                }
                fwrite(&b, 1, 1, outfile);
                b = 0;
            }
        }
    }
    free(msgf);
    fclose(infile);
    fclose(outfile);
}
int main(int argc, char *argv[]){
    if (argc!=2) {
        fprintf(stderr, "Error: Provide name of one source file.n");
        exit(EXIT_FAILURE);
    }
    char *clear_file = argv[1];
    convert2bin(clear_file);
    digest();
    printf("File successfully digested!n");
    return(0);
}

这段代码对于最大27字节的输入文件工作得很好,但超过27字节每次都会崩溃。

在尝试调试后,我发现问题在于strcpy(msgf, g_fxn());对函数g_fxn的调用,我的猜测是,问题来自为消息分配的大块内存,但似乎没有被解放。我猜这是因为代码工作时,有较少的调用g_fxn,但崩溃与更多的调用。

谁有什么想法,我怎么能解决这个问题?

msgf在函数digest()的while循环中被分配了很多次:

msgf = (char *) malloc(1 + (size)*3);

但是free()只在循环后被调用一次:

free(msgf);

这是我发现的至少一个内存泄漏

有一些函数是分配内存但不释放的,比如in -

char *g_fxn()char *bin(int n, int p)

编辑

当指针在这些函数中返回时,您可以使用goto语句。

下面的例子正好说明了如何解决你的问题——

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char lol(char c[]);
char lol(char c[])
{ 
  char a[6]="hello";
  char *p=malloc(sizeof(a));
  p=a;
  memcpy(c,p,strlen(a));
  goto DONE;
  DONE:
  free(p);
  return c;
}
int main()
{
   char b[5];
   lol(b);
   printf("%s",b);
  return 0;
}

如果函数分配内存并返回结果指针,而不是释放内存,那么调用函数必须确保一旦不再需要它就释放它。在strcpy(msgf, g_fxn())的情况下,它看起来像这样:

char *tmp = g_fxn();
strcpy(msgf, tmp);
free(tmp);

一般来说,每次从malloc()获得一个指向内存的指针时,一旦不再需要,你必须稍后将指向该内存的指针传递给free()

相关内容

  • 没有找到相关文章

最新更新