[comp.sys.ibm.pc] File buffer trashing data?

minakami@Neon.Stanford.EDU (Michael K. Minakami) (03/19/90)

I'm trying to debug a program and came across something unusal: fwrite()
is changing the contents of an array. The array has memory calloc'ed
for it at the beginning of the program, and when fwrite() is called it 
trashes everything started at array+4. I dereferenced the file pointer and
found a variable (blk?) that was pointing to that location. Also tracing
through fwrite, the array was being changed by a memcpy which (surprise)
copied the data I wanted to fwrite over the array. 

Does anyone know what's going on? 

Thanks
Michael

jimp@cognos.UUCP (Jim Patterson) (03/20/90)

In article <1990Mar19.054020.13581@Neon.Stanford.EDU> minakami@Neon.Stanford.EDU (Michael K. Minakami) writes:
>I'm trying to debug a program and came across something unusal: fwrite()
>is changing the contents of an array. The array has memory calloc'ed
>for it at the beginning of the program, and when fwrite() is called it 
>trashes everything started at array+4.

It sounds like someone has free'd your array before you're done with
it, and when you opened the file later, the buffer stdio uses was
given the area previously free'd. If you put a breakpoint in free
you might catch the offender. This might also happen without an
actual free if somehow you've corrupted the free list that malloc/free
maintain.
-- 
Jim Patterson                              Cognos Incorporated
UUCP:decvax!utzoo!dciem!nrcaer!cognos!jimp P.O. BOX 9707    
PHONE:(613)738-1440                        3755 Riverside Drive
                                           Ottawa, Ont  K1G 3Z4