473,382 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.
Feb 2 '08 #1
5 5238
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.
Feb 2 '08 #2
In article <sl*******************@nospam.invalid>,
Prad <no****@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
Feb 3 '08 #3
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
Feb 3 '08 #4
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
Feb 4 '08 #5
In article <fo**********@aioe.org>, testdemo <te******@irs.govwrote:
>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 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ken | last post by:
How can I reset the initial form variables that are set with session statements when clicking on a button? I tried this but the function was not called: <?PHP function reset_form($none) {...
2
by: John Smith | last post by:
Hello. Can anybody read this and come up with the solution? I would appreciate any input. In my form I have two fields (Field1 is select and Field2 is text input). Field1 has "onchange" event...
2
by: Charles M. Fish, Sr. | last post by:
I’m so tired from banging this problem around all day, I hope I can explain it succinctly & accurately. I want to execute a function immediately following a click on <input type="RESET"... ...
2
by: Matt | last post by:
i need to implement a clear button to clear all the fields in the form, but i am thinking i can just use reset button. <input type="reset" name="reset" value="CLEAR"> The first thought is...
2
by: Colin McKinnon | last post by:
Hi all, I've got two frames, one with a form ('main') and one with some clickable images ('tp'). The clickable images call a function in 'main' (code below) which in turn calls the .submit() or...
1
by: NancyASAP | last post by:
Thought I'd share this since it took me a long time to get it working. Thanks to a bunch of contributers in Google Groups who shared javascript, etc. The question was: How can I put a reset...
2
by: Lance | last post by:
I want to be able to reset a complex property in a PropertyGrid. I know that for properties that are ValueTypes you can include System.ComponentModel.DefaultValue in the declaration of the property....
16
by: sreemati | last post by:
Hi everyone, This is the scenario: I have two button - Submit and Reset Submit is used for validation and after validation is passed it passes it to another form to enter into database. ...
11
by: newbie | last post by:
i have a form in which a hidden field (initial value as '0', and my javascript set it to '1' when an event is trigged). In the same form, i have a reset field. But I realized that the hidden field...
3
by: artev | last post by:
if in a page I have a search form and I want use the method reset() for reinsert the default values, I notice that after a research, for the method reset(), the default values aren't more that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.