Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 文件IO上的分段错误_C_File Io_Segmentation Fault_Server - Fatal编程技术网

C 文件IO上的分段错误

C 文件IO上的分段错误,c,file-io,segmentation-fault,server,C,File Io,Segmentation Fault,Server,因此,我的任务是为非常简单的服务器实现加载功能。我真的不知道我的细分错误在哪里。我曾尝试使用GDB,但由于我使用telnet发送http头作为输入,因此很难从中获得任何结果 职能: //Loads a file into memory dynamically allocated on heap. //Stores address thereof in *content and length thereof in *length. bool load(FILE* file, BYTE** c

因此,我的任务是为非常简单的服务器实现加载功能。我真的不知道我的细分错误在哪里。我曾尝试使用GDB,但由于我使用telnet发送http头作为输入,因此很难从中获得任何结果

职能:

 //Loads a file into memory dynamically allocated on heap.
 //Stores address thereof in *content and length thereof in *length.

bool load(FILE* file, BYTE** content, size_t* length)
{
    //checks so file is open
    if(file == NULL)
    {
        return false;
    } 

    char* buffert = malloc(BYTES * sizeof(char));

    while(true)
    {

        // read into buffert
        fread(buffert, BYTES, sizeof(char), file);

        //store the pointer of this buffert in content
        *content = buffert;

        //update length
        length += 1;

        // checks for eof
        if(feof(file) != 0)
        {
            break;
        }
    }

   free(buffert);

   return true;
}
“…
BYTE
,我们确实将其定义为8位字符。”

谢谢

vs

BYTE**content
指向已取消分配的内存。只要
buffert
处于活动状态,您可能希望保持此数据处于活动状态,然后将其释放


另外,您可能打算写入
length+=BYTES
,因为您已经一次读取了整个文件。(还有,
while(true)
是不需要的)

Ahem…'自由(缓冲);'@马丁·詹姆斯,你能详细说明一下吗?我不太确定我是否在跟踪你。我在读取整个文件后释放缓冲区。因此,到那时,我已经将buffert的内容存储在指针中(是否存在错误?),并且不再使用它,对吗?存储在指针中的唯一内容是缓冲区的mamoery地址。单个指针可以保存文件的内容。它可以指向保存它的缓冲区。
  *content = buffert;
   free(buffert);