c - Why does a call to fread set my file pointer to null? -


i have file pointer valid before calling fread, , null after, , i'd know why.

here relevant code:

244 // open file reading 245 clo->heap_file = open_file(heap_path, "rb");  443 // allocate memory read page file 444 file_page = safe_malloc(clo->page_size);  446 // read page in memory file 447 read_page(file_page, clo);  void * safe_malloc(size_t size) {    void * mem_block = null;     mem_block = calloc(1, size);     if (mem_block == null) {       fprintf(stderr, "error: safe_malloc() failed allocate memory.\n");       exit(exit_failure);    }     return (mem_block); }  file * open_file(char * file_name, char * file_mode) {     file * fp;     char * err_msg = ((strcmp(file_mode, "rb") == 0)           ? "file not found"           : "file not created");      fp = fopen(file_name, file_mode);      /* print appropriate error message , exit if open failed */     if (fp == null) {        fprintf(stderr, "%s: %s\n", err_msg, file_name);        exit(exit_failure);     }      return fp; }  void read_page(clo_t * clo, void * file_page) {    fread(file_page, sizeof(size_t), clo->page_size, clo->heap_file);     if (ferror(clo->heap_file)) {       fprintf(stderr, "error: not read heap file!\n\n");       free(file_page);       destroy_clo(clo);       exit(exit_failure);    } } 

gdb trace:

(gdb) p clo->heap_file $1 = (file *) 0x603070 (gdb) s read_page (clo=0x6032d0, file_page=0x603010) @ dbquery.c:331 331        fread(file_page, clo->page_size, 1, clo->heap_file); (gdb) s 333        if (ferror(clo->heap_file)) { (gdb) p clo->heap_file $2 = (file *) 0x0 

and valgrind doesn't indicate i'm doing wrong either...

i'd think i'm pretty @ handling return values, making sure pointers valid, etc, 1 has me stumped.

you have 2 issues,

  1. change 447 read_page(file_page, clo); 447 read_page(clo, file_page); per definition of function.

  2. change fread(file_page, sizeof(size_t), clo->page_size, clo->heap_file); fread(file_page, 1, clo->page_size, clo->heap_file);. not use sizeof(size_t) 2nd parameter fread.

    your call try read sizeof(size_t) * clo->page_size bytes more have allocated file_page 1 * clo->page_size.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -