Thread: free allocated memory on interrupts

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    85

    free allocated memory on interrupts

    hello!

    i'm currently working a on program that uses linked lists to store information. it runs on the command line and does it's thing and when it's done, it calls a function to delete the entire linked list. now, as far as i know, if the user hits CTRL-C or simply closes the console, the program does not free the allocated memory.

    i've looked into signals, but am a little confused about where to begin. the material i've read so far does not allow me to use a function and pass arguments to it.

    has anybody encountered a similar problem and have a solution?

    i noticed this thread: http://cboard.cprogramming.com/showt...ghlight=CTRL+c

    however, i do not want to disable it, as i would like for them to have the option of exiting anytime they like.

    thanks for any help, if you need code posted, i'll put it up

    sean

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No need to panic - the OS will (should) return the memory whenever the program terminates. Sometimes it doesn't release all of it though. In that case, the remaining memory gets freed up by rebooting.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If you read the man page you find that you can pass a pointer to a function to execute when the signal occurs. As such:
    Code:
    #include <stdio.h>
    #include <signal.h>
    #include <string.h>
    
    void test(void);
    
    int main (void)
    {
      char buffer[1024];
      int keepalive=1;
    
      signal(SIGINT, (void *)test);
    
      do
      {
        printf("Type something (QUIT to quit): ");
        fgets(buffer, sizeof(buffer), stdin);
        if ( !strcmp(buffer, "QUIT\n") )
          keepalive = 0;
        printf("You typed: %s", buffer);
      }while(keepalive);
    
      return 0;
    }
    
    void test (void)
    {
      puts("HAHA YOU CAN'T ESCAPE THAT EASILY");
    }

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    85
    i've noticed that windows 2k doesn't quite free all of it up and it's such a hassle to reboot.

    and i've looked into that idea thantos, however, as i said, i need to pass arguments to the function, i can't just have a void/void function.

    also, does the operating system close any files open and resources used by sockets?

    thanks guys

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    After a program exits, the linux kernel keeps it in memory in case the parent calls a wait() call, but after the parent dies, the operating system frees all resources allocated by the program. That is linux though, the better operating system.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  2. Arrays and pointers to allocated memory
    By TriKri in forum C Programming
    Replies: 19
    Last Post: 07-23-2006, 10:52 AM
  3. Where are pointers allocated in memory?
    By sparks in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 12:52 PM
  4. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM