PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Why no reset() function?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Why no reset() function?

Réponse
 
LinkBack Outils de la discussion
Vieux 02/02/2008, 23h24   #1
Morris Dovey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why no reset() function?

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
  Réponse avec citation
Vieux 02/02/2008, 23h33   #2
Prad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Vieux 02/02/2008, 23h40   #3
Ian Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 03/02/2008, 00h12   #4
Richard Tobin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why no reset() function?

In article <slrnfq9vev.dk6.nospam@nospam.invalid>,
Prad <nospam@nospam.com> 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
  Réponse avec citation
Vieux 03/02/2008, 00h40   #5
Tomás Ó hÉilidhe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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_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
  Réponse avec citation
Vieux 04/02/2008, 10h44   #6
Robert Latest
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 04/02/2008, 13h50   #7
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why no reset() function?

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
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 04/02/2008, 15h50   #8
testdemo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why no reset() function?

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
> esosman@ieee-dot-org.invalid


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h44.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20884 seconds with 16 queries