Why am I getting this “double freedom or corruption” memory access error?

I get the following type of error. I know that this has something to do with me, improperly accessing memory, but I don’t know for sure. Please help me see where I made a mistake.

* note I have simplified my function, and it is not clear what variables do, I just need to know how I improperly implement the function or where I use memory access incorrectly.

int my_function(char const *file_name, size_t max)
        {

        myStruct.pStore = fopen(file_name,"w+");      //pStore is a FILE* 
        myStruct.max = max;                 

        // fill the with zeros ('0')
        int numberOfZeros = max*SIZE;
        char zeros[numberOfZeros];                      

        int i=0;
        while(i<numberOfZeros)         // insert zero 
        {
                zeros[i]='0';
                i++;
        }
        fwrite(zeros,sizeof(char),numberOfZeros,myStruct.pStore);
        fclose(myStruct.pStore);

        return EXIT_SUCCESS; 

The error I give is:

*** glibc detected *** /home/.../: double free or corruption (top): 0x0804c008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x73e42)[0xb7e82e42]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0xb7e72384]
/home/2012/spatar/cs/specs/release[0x80486b0]
/home/2012/spatar/cs/specs/release[0x8048acd]
/home/2012/spatar/cs/specs/release[0x8048af0]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e284d3]
/home/2012/spatar/cs/specs/release[0x80484e1]
 ======= Memory map: ========
08048000-0804a000 r-xp 00000000 00:3b 2331829    /home/2012/spatar/cs/Aspecs/release
0804a000-0804b000 r--p 00001000 00:3b 2331829    /home/2012/spatar/cs/specs/release
0804b000-0804c000 rw-p 00002000 00:3b 2331829    /home/2012/spatar/cs/specs/release
0804c000-0806d000 rw-p 00000000 00:00 0          [heap]
b7e0e000-b7e0f000 rw-p 00000000 00:00 0 
b7e0f000-b7fae000 r-xp 00000000 00:11 5415       /lib/i386-linux-gnu/libc-2.15.so
b7fae000-b7fb0000 r--p 0019f000 00:11 5415       /lib/i386-linux-gnu/libc-2.15.so
b7fb0000-b7fb1000 rw-p 001a1000 00:11 5415       /lib/i386-linux-gnu/libc-2.15.so
b7fb1000-b7fb4000 rw-p 00000000 00:00 0 
b7fbc000-b7fd8000 r-xp 00000000 00:11 5426       /lib/i386-linux-gnu/libgcc_s.so.1
b7fd8000-b7fd9000 r--p 0001b000 00:11 5426       /lib/i386-linux-gnu/libgcc_s.so.1
b7fd9000-b7fda000 rw-p 0001c000 00:11 5426       /lib/i386-linux-gnu/libgcc_s.so.1
b7fda000-b7fdd000 rw-p 00000000 00:00 0 
b7fdd000-b7fde000 r-xp 00000000 00:00 0          [vdso]
b7fde000-b7ffe000 r-xp 00000000 00:11 5405       /lib/i386-linux-gnu/ld-2.15.so
b7ffe000-b7fff000 r--p 0001f000 00:11 5405       /lib/i386-linux-gnu/ld-2.15.so
b7fff000-b8000000 rw-p 00020000 00:11 5405       /lib/i386-linux-gnu/ld-2.15.so
bffdf000-c0000000 rw-p 00000000 00:00 0          [stack]
+5
source share
2 answers

It looks like you are trying to free a memory that has already been freed or dereferenced.

Link your program to efence or run it with valgrind .

, .

+5

, , - , .

free (x) x. - (x) , , , free (x) .

- gdb , .

my_function , malloc . , while . - . , (), , malloc/free/strdup ..

+2

All Articles