Question

I am trying to declare a dynamic array so that it can read and store specifically 100 bytes of contents into the char *buffer. It works well when it is the fread function is has a size_t nmemb of is a small amount, like 10. But when I set it higher it displays some of the contents but gives me a glibc detected error.

This is how it works if in the fread function I set it to a small number

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{

FILE *fp;
int num;   
char *buffer;
buffer=(char*)malloc(sizeof(char)*num);
memset(buffer.0,sizeof(char)*num);
   /* Open file for reading */
   fp = fopen("hello.txt", "r");

   /* Read and display data */
   fread(buffer, 1, 10, fp);
   printf("%s\n", buffer);
free(buffer);
   fclose(fp);

   return(0);
}

/***************   Ouput   *****************/

travis@ubuntu:~$ g++ -o fileread2 fileread2.c

travis@ubuntu:~$ ./fileread2

asdkfjasdk

This is what happens when I change it the 3rd arg in fread to a 100:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

    int main()
        {   
FILE *fp;
int num;   
char *buffer;
buffer=(char*)malloc(sizeof(char)*num);
memset(buffer.0,sizeof(char)*num);

           /* Open file for reading */
           fp = fopen("hello.txt", "r");

           /* Read and display data */
           fread(buffer, 1, 100, fp);/*This is all I changed*/
           printf("%s\n", buffer);
free(buffer);
           fclose(fp);

           return(0);
        }

/*************    Output   ********************/

travis@ubuntu:~$ g++ -o fileread2 fileread2.c

travis@ubuntu:~$ ./fileread2

asdkfjasdk;ljfkadsljck;lasm
asdkjdasfkja�s;kljfa
sdfksadjf;kljads;lkjads;lkjfadslkja
dsasdkfj;dsjf;a�
*** glibc detected *** ./fileread2: free(): invalid next size (fast): 0x0000000000d88010 ***

======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fb2aa586b96]
./fileread2[0x4007c8]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fb2aa52976d]
./fileread2[0x400659]
======= Memory map: ========
00400000-00401000 r-xp 00000000 07:00 785332                             /home/travis/fileread2
00600000-00601000 r--p 00000000 07:00 785332                             /home/travis/fileread2
00601000-00602000 rw-p 00001000 07:00 785332                             /home/travis/fileread2
00d88000-00da9000 rw-p 00000000 00:00 0                                  [heap]
7fb2aa2f0000-7fb2aa305000 r-xp 00000000 07:00 265977                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fb2aa305000-7fb2aa504000 ---p 00015000 07:00 265977                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fb2aa504000-7fb2aa505000 r--p 00014000 07:00 265977                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fb2aa505000-7fb2aa506000 rw-p 00015000 07:00 265977                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fb2aa508000-7fb2aa6bd000 r-xp 00000000 07:00 281111                     /lib/x86_64-linux-gnu/libc-2.15.so
7fb2aa6bd000-7fb2aa8bd000 ---p 001b5000 07:00 281111                     /lib/x86_64-linux-gnu/libc-2.15.so
7fb2aa8bd000-7fb2aa8c1000 r--p 001b5000 07:00 281111                     /lib/x86_64-linux-gnu/libc-2.15.so
7fb2aa8c1000-7fb2aa8c3000 rw-p 001b9000 07:00 281111                     /lib/x86_64-linux-gnu/libc-2.15.so
7fb2aa8c3000-7fb2aa8c8000 rw-p 00000000 00:00 0 
7fb2aa8c8000-7fb2aa8ea000 r-xp 00000000 07:00 281123                     /lib/x86_64-linux-gnu/ld-2.15.so
7fb2aaae6000-7fb2aaaea000 rw-p 00000000 00:00 0 
7fb2aaaea000-7fb2aaaeb000 r--p 00022000 07:00 281123                     /lib/x86_64-linux-gnu/ld-2.15.so
7fb2aaaeb000-7fb2aaaed000 rw-p 00023000 07:00 281123                     /lib/x86_64-linux-gnu/ld-2.15.so
7fb2aaaed000-7fb2aaaf1000 rw-p 00000000 00:00 0 
7fffdf9a2000-7fffdf9c3000 rw-p 00000000 00:00 0                          [stack]
7fffdfa00000-7fffdfa02000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

/*****        End of Output      ******/

Can someone help me out on this please?? Thanks.

Was it helpful?

Solution

You are calling malloc with the product of num and sizeof(char). The value num is not initialized to anything. So you are allocating a random amount of memory when you call malloc. You should set num equal to the number of bytes you intend to read in or greater. I suggest modifying your code along these lines

num = 100;
buffer = malloc(sizeof(char)*num))
int err = fread(buffer,sizeof(char),num,fp);

In this way you will not read in more data than is available in buffer. Also, you should check the return values of fread and malloc for possible errors. malloc can return the NULL pointer on error, and fread can return zero. You'll need to use ferror to get the exact error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top