free allocated memory on interrupts

This is a discussion on free allocated memory on interrupts within the C Programming forums, part of the General Programming Boards category; hello! i'm currently working a on program that uses linked lists to store information. it runs on the command line ...

  1. #1
    Registered User
    Join Date
    Jul 2003

    free allocated memory on interrupts


    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: How to prevent exiting from running program

    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


  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    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.
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            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
    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:
    #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);
        printf("Type something (QUIT to quit): ");
        fgets(buffer, sizeof(buffer), stdin);
        if ( !strcmp(buffer, "QUIT\n") )
          keepalive = 0;
        printf("You typed: %s", buffer);
      return 0;
    void test (void)

  4. #4
    Registered User
    Join Date
    Jul 2003
    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
    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://
    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, 01:57 AM
  2. Arrays and pointers to allocated memory
    By TriKri in forum C Programming
    Replies: 19
    Last Post: 07-23-2006, 11:52 AM
  3. Where are pointers allocated in memory?
    By sparks in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 01:52 PM
  4. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21