Why no reset() function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Prad

    #1

    Why no reset() function?

    Hello,

    In BASIC there is a very useful function called RESET that clears out
    all the stored variables and restores memory state to startup.

    Why is there not a similar function in C? It should free up all heap
    allocations without having to free them individually.

    Often programs will have memory leaks, so if they are run over long
    times then it's necessary to restart them sometimes. With a reset()
    function, they could effectively restart themselves by freeing their
    memory without needing to be closed and reopened.


  • Ian Collins

    #2
    Re: Why no reset() function?

    Prad wrote:
    Hello,
    >
    In BASIC there is a very useful function called RESET that clears out
    all the stored variables and restores memory state to startup.
    >
    Why is there not a similar function in C? It should free up all heap
    allocations without having to free them individually.
    >
    Because it would probably be disastrous. If you wish to do this, just
    run your application from another, exit periodically and have the parent
    restart the application.
    Often programs will have memory leaks, so if they are run over long
    times then it's necessary to restart them sometimes. With a reset()
    function, they could effectively restart themselves by freeing their
    memory without needing to be closed and reopened.
    >
    No, it is simply necessary to clean up your mess and not leak memory.

    --
    Ian Collins.

    Comment

    • Richard Tobin

      #3
      Re: Why no reset() function?

      In article <slrnfq9vev.dk6 .nospam@nospam. invalid>,
      Prad <nospam@nospam. comwrote:
      >In BASIC there is a very useful function called RESET that clears out
      >all the stored variables and restores memory state to startup.
      I don't recall that in any version of Basic I used, but it was
      a long time ago.
      >Why is there not a similar function in C? It should free up all heap
      >allocations without having to free them individually.
      Why not just run the program again?

      Presumably it existed in whatever Basic you used because it's an
      interactive, interpreted environment. C programming isn't usually
      done like that.

      -- Richard
      --
      :wq

      Comment

      • =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=

        #4
        Re: Why no reset() function?

        Prad:
        Hello,
        >
        In BASIC there is a very useful function called RESET that clears out
        all the stored variables and restores memory state to startup.
        >
        Why is there not a similar function in C? It should free up all heap
        allocations without having to free them individually.

        Such a thing could be coded quite easily. I'd hijack "malloc" and keep a
        table of all allocated memory, maybe something like:

        void *allocs[256] = {};

        void **p_current_all oc = allocs;

        void *my_malloc(size _t const len)
        {
        return *p_current_allo c++ = malloc(len);
        }

        void Reset(void)
        {
        void **p = allocs;

        do free(*p++);
        while (p_current_allo c != p);

        p_current_alloc = allocs;
        }

        Of course tho, you'd be better off using malloc properly.

        --
        Tomás Ó hÉilidhe

        Comment

        • Robert Latest

          #5
          Re: Why no reset() function?

          Prad wrote:
          In BASIC there is a very useful function called RESET that clears out
          all the stored variables and restores memory state to startup.
          You could try setjmp() and longjmp(), but that won't clear dynamically
          allocated storage.
          Often programs will have memory leaks, so if they are run over long
          times then it's necessary to restart them sometimes.
          s/restart/fix
          s/sometimes/once

          robert

          Comment

          • dj3vande@csclub.uwaterloo.ca.invalid

            #6
            Re: Why no reset() function?

            In article <fo7c81$23v$1@a ioe.org>, testdemo <testdemo@irs.g ovwrote:
            >Just testing...
            You fail.
            Test posts go in newsgroups whose names end in ".test".
            Quoting an entire post to top-post a content-free comment is a Bad Idea
            no matter where you do it.
            Bogus email addresses should not resolve to real domains.


            dave

            --
            Dave Vandervies dj3vande at eskimo dot com
            By the time you get a result out of scanf with %d, the result *IS* an integer,
            regardless of what was input, with the exception of a few platforms where an
            int can hold a live goat. --Gordon Burditt in CLC

            Comment

            Working...