Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #16601 > unrolled thread
| Started by | Turtle Wizard <0wl256NOSPAM@gmail.com> |
|---|---|
| First post | 2012-02-01 21:10 +0900 |
| Last post | 2012-02-01 21:10 +0900 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.c
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Restarting my program Turtle Wizard <0wl256NOSPAM@gmail.com> - 2012-02-01 21:10 +0900
| From | Turtle Wizard <0wl256NOSPAM@gmail.com> |
|---|---|
| Date | 2012-02-01 21:10 +0900 |
| Subject | Re: Restarting my program |
| Message-ID | <sa6ty3ahi04.fsf@gmail.com> |
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> luser- -droog <mijoryx@yahoo.com> writes:
>
>> On Feb 2, 1:34 am, Dr Nick <3-nos...@temporary-address.org.uk> wrote:
>>> luser- -droog <mijo...@yahoo.com> writes:
>>> > On Feb 1, 4:23 pm, Willem <wil...@turtle.stack.nl> wrote:
>>> >> luser- -droog wrote:
>>>
>>> >> ) On Feb 1, 12:45?pm, kevin donne <nos...@nospam.com> wrote:
>>> >> )> Whatup,
>>> >> )>
>>> >> )> I have a program that sometimes I need to restart from within
>>> >> the program
>>> >> )> itself.
>>> >> )>
>>> >> )> Obviously I can store away argc and argv and just call main() again,
>>> >> )> however this does not reinitialize any static variables in my code and
>>> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>> >> )> any libraries.
>>> >> )>
>>> >> )> How can I achieve this please? Is there some way with setjump()?
>>> >> )>
>>> >> )> TIA
>>> >> )
>>> >> ) You can use setjmp for this. But you may have to reorganize
>>> >> ) to get it to work right.
>>>
>>> >> Since when does longjmp restore static variables ?
>>>
>>> > Hence the part about "reorganizing". OP will have to explicitly
>>> > initialize these after the setjmp call in main.
>>>
>>> In which case he might as well "just":
>>> * replace all static variables with globals (perhaps in a structure)
>>> * write an "initialise global state" function
>>> * make the main program into a tiny one that checks (NB, checks, not
>>> reads) its parameters and environment as appropriate and saves argc and
>>> argv and then calls do_everything(global.argc,global.argv)
>>> * call do_everything from anywhere when you need to start again
>>>
>>> If this starts causing the stack to grow enormously, then some sort of
>>> return up the chain or - at that stage - setjmp/longjump may be called for.
>>
>> Yeah. Pretty much. He'll probably also need to destroy all structures,
>> flush output files, reset terminal. The controlling script idea is
>> starting to sound easier. But none of this sounds impossible.
>
> With C99, I don't think it is possible to return the program to it's
> initial sate. For example, stream orientation is not determined at
> start-up and can be set by the first IO operation on the stream, but
> there is no way to set the orientation back to this unspecified state.
You can do this both in C an C++ :
test.c:
-------
static int i = 0;
int main()
{
if (1)
#include "test2.c"
return 0;
}
test2.c:
--------
static int i = 1;
This should restore your program to any state but there
are other ways to do it (e.g. managing your stack.)
>
> In addition to the huge lists of things you'd have to reset (and you are
> bound to forget something) there is the problem of atexit handlers.
>
You can also reload your environment.
int main(int argc, char *argv[], char **envp)
{ return 0; }
> Having said all that, if the program is well-designed it is possible
> that writing an initialisation function and putting a loop round the
> body of main would fit the bill. Some program assume very little about
> their initial state.
This is where goto comes in.
TW
Back to top | Article view | comp.lang.c
csiph-web