guide mallocs a buffer an passes it to io_read. The program doesn't
call free() on the malloced buffer but when I add it it doesn't complain.
/* Read */
err = io_read (f, &buf, &amount, -1, amount);
if (err)
error (1, errno, "Could not read from file %s", argv[1]);
buf[amount] = '\0';
mach_port_deallocate (mach_task_self (), f);
/* Output */
printf ("%s", buf);
free(buf); /* added by me */
But when I try to do the same in my translator, I get a "free(): invalid pointer"
error. Here is the relevant portion of my code.
filebuf = malloc(s.st_size + 1);
err = io_read(innerdir, &filebuf, &amt_written, 0, s.st_size);
free(filebuf);
I put the free right after the io_read to test whether something I was doing after
reading it into the buffer might be causing the error, but it still occurred.
The program works fine without mallocing the buffer at all, and it works fine when
I malloc but don't free. But both of those would seem to create memory leaks. Can
anyone help me understand what's going on here?
Thanks
Andrew Eggenberger