Why no reset() function?

Discussion in 'C Programming' started by Morris Dovey, Feb 2, 2008.

  1. Morris Dovey

    Morris Dovey Guest

    Prad wrote:

    > 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.


    A memory leak constitutes a program error. Programs without such
    errors don't need any of the remedies you're suggesting.

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto
     
    Morris Dovey, Feb 2, 2008
    #1
    1. Advertising

  2. Morris Dovey

    Prad Guest

    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.
     
    Prad, Feb 2, 2008
    #2
    1. Advertising

  3. Morris Dovey

    Ian Collins Guest

    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.
     
    Ian Collins, Feb 2, 2008
    #3
  4. In article <>,
    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.


    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
     
    Richard Tobin, Feb 3, 2008
    #4
  5. 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_alloc = allocs;

    void *my_malloc(size_t const len)
    {
    return *p_current_alloc++ = malloc(len);
    }

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

    do free(*p++);
    while (p_current_alloc != p);

    p_current_alloc = allocs;
    }

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

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Feb 3, 2008
    #5
  6. 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
     
    Robert Latest, Feb 4, 2008
    #6
  7. Morris Dovey

    Eric Sosman Guest

    Robert Latest wrote:
    > 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


    The counsel of perfection is unassailable, as far as it
    goes -- but only that far. Programming is largely an economic
    activity: You write (and enhance, and fix, and rewrite ...) a
    program to accomplish some purpose or other, the idea being
    that the effort expended on the program will be less than it
    would have taken to accomplish the same purpose without the
    program's aid.

    ... which means that the decision to spend further effort
    fixing it or to spend less effort on a sloppy work-around is
    also an economic decision. It calls for a certain amount of
    guesswork about the likely future use(s) of the program, too:
    How much will it hurt if I use the work-around today and then
    wind up needing to do the full-scale fix later anyhow? But it's
    not a law of the universe that every bug must be squeezed out
    of every program! Sometimes the expedient course, and even the
    sensible course, is to use the program as it stands, bugs and
    all, and to tolerate those that you must.

    And even if you find this attitude defeatist and perhaps
    loathsome, consider: The fact that a program consumes more and
    more memory as time passes and eventually exhausts it is not
    proof of a memory leak. The key word is "fragmentation," and
    in TAOCP section 2.5 Knuth reports on a proof by J.M. Robson
    that *all* allocation methods are vulnerable to fragmentation
    unless they are able to move allocated blocks: "[...] even when
    blocks are restricted to be of sizes 1 and 2, overflow might
    occur with memory only about 2/3 full, no matter what allocation
    algorithm is used!"

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 4, 2008
    #7
  8. Morris Dovey

    testdemo Guest

    Just testing...

    Eric Sosman Wrote:

    > Robert Latest wrote:
    > > 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

    >
    > The counsel of perfection is unassailable, as far as it
    > goes -- but only that far. Programming is largely an economic
    > activity: You write (and enhance, and fix, and rewrite ...) a
    > program to accomplish some purpose or other, the idea being
    > that the effort expended on the program will be less than it
    > would have taken to accomplish the same purpose without the
    > program's aid.
    >
    > ... which means that the decision to spend further effort
    > fixing it or to spend less effort on a sloppy work-around is
    > also an economic decision. It calls for a certain amount of
    > guesswork about the likely future use(s) of the program, too:
    > How much will it hurt if I use the work-around today and then
    > wind up needing to do the full-scale fix later anyhow? But it's
    > not a law of the universe that every bug must be squeezed out
    > of every program! Sometimes the expedient course, and even the
    > sensible course, is to use the program as it stands, bugs and
    > all, and to tolerate those that you must.
    >
    > And even if you find this attitude defeatist and perhaps
    > loathsome, consider: The fact that a program consumes more and
    > more memory as time passes and eventually exhausts it is not
    > proof of a memory leak. The key word is "fragmentation," and
    > in TAOCP section 2.5 Knuth reports on a proof by J.M. Robson
    > that *all* allocation methods are vulnerable to fragmentation
    > unless they are able to move allocated blocks: "[...] even when
    > blocks are restricted to be of sizes 1 and 2, overflow might
    > occur with memory only about 2/3 full, no matter what allocation
    > algorithm is used!"
    >
    > --
    > Eric Sosman
    > lid
     
    testdemo, Feb 4, 2008
    #8
  9. Morris Dovey

    Guest

    In article <fo7c81$23v$>, testdemo <> wrote:
    >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
     
    , Feb 4, 2008
    #9
  10. Eric Sosman wrote:

    > And even if you find this attitude defeatist and perhaps
    > loathsome,


    I don't. It's simple pragmatism. But I had the impression that the OP didn't
    want to worry about memory management at all.

    robert
     
    Robert Latest, Feb 5, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grant Rettke
    Replies:
    0
    Views:
    443
    Grant Rettke
    Mar 16, 2005
  2. Mike Austin
    Replies:
    4
    Views:
    1,934
    strick
    Dec 8, 2007
  3. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    466
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    917
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,031
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page