c - Segmentation fault on file IO -


so task implement load function very simple server. , cant figure out segmentation fault is. i've tried use gdb since use telnet send http headers input i've had hard time getting results it.

the function:

 //loads file memory dynamically allocated on heap.  //stores address thereof in *content , length thereof in *length.  bool load(file* file, byte** content, size_t* length) {     //checks file open     if(file == null)     {         return false;     }       char* buffert = malloc(bytes * sizeof(char));      while(true)     {          // read buffert         fread(buffert, bytes, sizeof(char), file);          //store pointer of buffert in content         *content = buffert;          //update length         length += 1;          // checks eof         if(feof(file) != 0)         {             break;         }     }     free(buffert);     return true; } 

"...byte, we’ve indeed defined 8-bit char."

thx!!

  *content = buffert; 

vs.

   free(buffert); 

byte** content points memory de-allocated. want keep data alive long buffert alive, , deallocate then.

plus, meant write length += bytes , since reading whole file once. (also, while(true) un-needed)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -