memory allocation is automatically freed when the scope in which it was allocated ends? seems to be the case, since if i use another function, lets call it f_x (char *buffer, int size);, to allocate on buffer for size, i get garbage on the exit of f_x if i redisplay the buffer...
Code:
// choppy example
void f_x (char *buffer, int size);
void free_x (char *buffer);
int main (void)
{
char *buf;
f_x (buf, whatever_size);
// here data is not okay...
free_x (buf);
}
void f_x (char *buffer, int size)
{
if((buffer = calloc (size, 1)) == NULL) //bomb out...
// read something into buffer from a file
// here data is okay
}
void free_x (char *buffer)
{
free (buffer);
}
anyways, something like that... i figure that it works against my assumption since Xalloc has to return a void pointer and have casting/reassignment to the new pointer... [like in my usage]... i want to do this because i want to have a function that opens a file, allocs a buffer, reads the file into that buffer, and closes the file... and one that opens a file, writes the buffer to the file, frees the buffer, and closes the file... all in one handy reusable call...
[but the data is altered in between the file loading and unloading] so i need the file size to be returned [and saved], which is the return value of my load/unload functions. my intermediate functions alter the data and a running count of the buffer size is needed... i could use a structure for file-length and buffer and have it be returned in my load/unload routines, but i wanted to check with you guys for alternatives just in case. thank you...