gpt4 book ai didi

c - 释放为 C 中的一个很长的字符串 (char*) 分配的内存?

转载 作者:太空宇宙 更新时间:2023-11-04 08:24:33 24 4
gpt4 key购买 nike

拜托,我需要帮助来释放分配给一个非常长的字符串的内存。我试图将代码缩减为包含我遇到的问题的这个小片段:

#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) = '\0';
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);

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

关于c - 释放为 C 中的一个很长的字符串 (char*) 分配的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31272416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com