On 4 Juni, 13:00, Chris Dollin <chris.dol...@hp.com> wrote:
> Joakim Hove wrote:
> >> Is there away to get C to release the memory allocated before it exits?
>
> > Well certainly YES.
>
> Using what? `free` is under no obligation to hand back memory to the
> OS, as far as I can tell.
>
> --
> "It is seldom good news." * * *~Crystal Ball~, /The Tough Guide to Fantasyland/
>
> Hewlett-Packard Limited * * Cain Road, Bracknell, * * * * * * * *registered no:
> registered office: * * * * *Berks RG12 1HN * * * * * * * * * * * 690597 England
The code/program is several about 2000 lines with several long
functions so I didn't want to post it all here.
Since the "problem" is more theoretical I thought I'd ask a general
question that Chris I think answered.
If free doesn't hand back memory to the OS, how can I force it to do
so?
I wrote a little program that does a system call from C and it works
(see below). When I insert this in my main C program just before
exiting main() and after freeing all allocated memory the system()
call returns -1 and the error "not enough memory". So I think that
when the memory is freed it's not returned to the OS before the
program ends. Is there a solution to this problem?
This file that I'm trying to zip is 504 MB so it needs a lot of
memory.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(void) {
int status = -9999;
char cmd[] = "/bin/bzip2 /home/c_programming/trunk/complete/
ECMWF_NWP_20080910-0000-00.NC";
printf("Zipping file using bzip2 via system call...\n");
status = system(cmd);
printf("%d\n",status);
if(status != 0) {
perror("Error found!");
printf("errno = %d.\n", errno);
fprintf(stderr,"Failed to zip output file");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
Thanks,
/Marston
|