gpt4 book ai didi

c - free() 在子进程释放内存时造成麻烦

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

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<malloc.h>

int main(int argc, char* argv[]){
int fd,fd1,fd2,fd3;
int fork_id1,fork_id2,fork_id3;
char *buffer = NULL;
char *buff = NULL;
char *loc = NULL;
int no_bytes_read = 0;
int no_bytes_write = 0;
int total_file_size = 0;
int half_file_size = 0;
int count = 0;
int ret_val = 0;

if(argc < 5){
printf("Enter 1 i/p and 3 o/p files");
return -1;
}

fd = open(argv[1],O_RDONLY);
if(fd == -1){
printf("\nRead file desc not created!\n");
return -1;
}

total_file_size = lseek(fd,0,SEEK_END);
half_file_size = (total_file_size / 2);
lseek(fd,0,SEEK_SET);
printf("\n Total File size is : %d \n",total_file_size);

buffer = (char *)malloc((sizeof(char) * total_file_size));
if(buffer == NULL){
printf("\n Cant allocate memory for buffer \n");
return -1;
}

no_bytes_read = read(fd,buffer,total_file_size);

fork_id1 = fork();
if(fork_id1 == 0){

fd1 = open(argv[2],O_WRONLY);
if(fd1 == -1){
printf("\n write file des for child1 not created!\n");
return -1;
}

buff = (char *)malloc((sizeof(char) * half_file_size));
if(buff == NULL){
printf("\n buff not created for child1 \n");
return -1;
}
loc = buff;

for(count = 0; count <= half_file_size; count++){
*buff++ = *buffer++;
}
buff = loc;

no_bytes_write = write(fd1,loc,half_file_size);
free(buff);
buff = NULL;
}
else{
wait();
fork_id2 = fork();
if(fork_id2 == 0){

fd2 = open(argv[3],O_WRONLY);
if(fd2 == -1){
printf("\n write file des not created for child2 \n");
return -1;
}

buff = (char *)malloc((sizeof(char) * half_file_size));
if(buff == NULL){
printf("\n buff not created for child1 \n");
return -1;
}
loc = buff;

for(count = half_file_size+1 ; count <= total_file_size; count++){
*buff++ = buffer[count];
}
buff = loc
no_bytes_write = write(fd2,loc,half_file_size);
free(buff);
}
else{
wait();
fork_id3 = fork();
if(fork_id3 == 0){

fd3 = open(argv[4],O_WRONLY);
if(fd3 == -1){
printf("\n write file des not created for child3 \n");
return -1;
}

buff = (char *)malloc((sizeof(char)* total_file_size));
if(buff == NULL){
printf("\n buff not created for child1 \n");
return -1;
}
loc = buff;

for(count = 0 ; count <= total_file_size; count++){
*buff++ = *buffer++;
}
buff = loc;
no_bytes_write = write(fd3,loc,total_file_size);
free(buff);
}
else{
wait(); //TODO
//free(buffer);
printf("\n Parent \n");
/* To compare the files */
//ret_val = validate(argv[1],argv[2],argv[3],argv[4],total_file_size);
//if(ret_val == -1){
// return -1;
//}
}
}
}
return 0;
}

我正在将整个文件复制到缓冲区中,并将第一个字节复制到文件 A,将第二个字节复制到文件 B,将完整内容复制到文件 C。在每个 child 中,我正在为临时文件分配内存缓冲区,当我试图释放时出现段错误。

最佳答案

这是原因:

 for(count = 0; count <= half_file_size; count++){
*buff++ = *buffer++;
...
free(buff);

你的循环正在改变你分配内存的指针。您只能在具有 malloc 返回的原始值的指针上使用 free

关于c - free() 在子进程释放内存时造成麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15489367/

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