Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!weretis.net!feeder4.news.weretis.net!nuzba.szn.dk!news.szn.dk!pnx.dk!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail From: Turtle Wizard <0wl256NOSPAM@gmail.com> Newsgroups: comp.lang.c Subject: Re: Restarting my program Organization: Faerun References: <40fdb34c-c810-451e-a14f-2ffa6d9a185f@n10g2000yqf.googlegroups.com> <1d38c886-a329-47ed-b63c-a04ba436f334@o1g2000yqb.googlegroups.com> <87hbcmx53f.fsf@temporary-address.org.uk> <45022d16-495b-43f8-a912-ed4d127bfc29@r16g2000yql.googlegroups.com> <0.e74a9dacbf2bec613014.20110202141709GMT.87zkqe34ii.fsf@bsb.me.uk> Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAMFBMVEWXl5d7e3thYWH4+Pgf Hx+0tLSkpKSEhIRycnJOTk7///84ODjp6enZ2dnJyckICAhNWFMTAAAB2ElEQVQ4jY3TP0jj UBzA8YpUXQTP6dw0IFgObnELqdNbpIPBw7q08ZZ0O08FURdRkVMX/4DDFScp3vCjJyIdlIzl llLDgejUF1x6w9Fmuv35fm2Sl/QFzu+UvE/73q9tmgCvLwDPdR6AncX7hA/tWtmhPIB0MwJ2 1mx1rzQnAk9H3/Pdq5IWgcXz2XXcqqp27wMoElJkvHa1B1imguvMeOyF2XNW5PkjdOBnPywp PKqPWuwjpAMof4Kv+Ors0AfCjkFMpVXTWwjKRYVlttXpABoPi4cWjxDGJpar4gz6Z2+XEFwm xHUfBVyPTSmdWTP3CjgtAVdz4/u4Tm53TJgJwQ82eYJgTSQpXOcEXN7MZxAgVAB4eCzguLGA HzAW8LuKhUNeLGzw3g5QfsqyG9q0C7kolGx9g53SpvO5B1x9M/VvvO02B4ejUOhLWfc7BtXf r8uQSBiNwdS3mrTVg75cW7s4/SsdDi/J2pF11pLGhd9JNVW5y0UBz+G94/0fpr0koF4SuF4S LHhJUAgVAT953F9jc7iA2ykHZjsEWxUEfNfUiEEF2ENFBHxKdgfydQFpfRUBH7gVs6EK0JwF H8x88HNwKGl1fyujASHg+Yf7f/4OqB7guFTsBK8LQpBO0VdhGAAAAABJRU5ErkJggg== X-Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAMFBMVEWXl5d7e3thYWH4+Pgf Hx+0tLSkpKSEhIRycnJOTk7///84ODjp6enZ2dnJyckICAhNWFMTAAAB2ElEQVQ4jY3TP0jj UBzA8YpUXQTP6dw0IFgObnELqdNbpIPBw7q08ZZ0O08FURdRkVMX/4DDFScp3vCjJyIdlIzl llLDgejUF1x6w9Fmuv35fm2Sl/QFzu+UvE/73q9tmgCvLwDPdR6AncX7hA/tWtmhPIB0MwJ2 1mx1rzQnAk9H3/Pdq5IWgcXz2XXcqqp27wMoElJkvHa1B1imguvMeOyF2XNW5PkjdOBnPywp PKqPWuwjpAMof4Kv+Ors0AfCjkFMpVXTWwjKRYVlttXpABoPi4cWjxDGJpar4gz6Z2+XEFwm xHUfBVyPTSmdWTP3CjgtAVdz4/u4Tm53TJgJwQ82eYJgTSQpXOcEXN7MZxAgVAB4eCzguLGA HzAW8LuKhUNeLGzw3g5QfsqyG9q0C7kolGx9g53SpvO5B1x9M/VvvO02B4ejUOhLWfc7BtXf r8uQSBiNwdS3mrTVg75cW7s4/SsdDi/J2pF11pLGhd9JNVW5y0UBz+G94/0fpr0koF4SuF4S LHhJUAgVAT953F9jc7iA2ykHZjsEWxUEfNfUiEEF2ENFBHxKdgfydQFpfRUBH7gVs6EK0JwF H8x88HNwKGl1fyujASHg+Yf7f/4OqB7guFTsBK8LQpBO0VdhGAAAAABJRU5ErkJggg== Date: Wed, 01 Feb 2012 21:10:03 +0900 Message-ID: User-Agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.92 (gnu/linux) Cancel-Lock: sha1:VftHVQVM34B10yDknM3w0t6H9Zo= MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Lines: 90 NNTP-Posting-Host: 94.225.69.182 X-Trace: news.sunsite.dk DXC=7GC9AYmSWTJI[NbmUoDmmGYSB=nbEKnkKDoEJ]`MUjhMKPVSLA_UhIJX:m4R:`XhkMCGLNFUj^I4NgdB@jc:J0YGkA:a99N36nD X-Complaints-To: staff@sunsite.dk Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:16601 Ben Bacarisse writes: > luser- -droog writes: > >> On Feb 2, 1:34 am, Dr Nick <3-nos...@temporary-address.org.uk> wrote: >>> luser- -droog writes: >>> > On Feb 1, 4:23 pm, Willem wrote: >>> >> luser- -droog wrote: >>> >>> >> ) On Feb 1, 12:45?pm, kevin donne 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