Thread: How to Restart or Clean all Variable in memory?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184

    How to Restart or Clean all Variable in memory?

    Hi,,

    how to restart (self) application ?

    I Need Clean all variable allocateds, and start again the program...

    I Know this isn't the best method to a program, but i need urgent..

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Start a new instance of your program (and close the old one). Everything you've done up until then will be lost.
    There is no other way, other than freeing the memory and resources you use (don't create memory and/or resource leaks!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    Hi,,

    how to restart (self) application ?

    I Need Clean all variable allocateds, and start again the program...

    I Know this isn't the best method to a program, but i need urgent..
    As I stated earilier (in the other thread - so I guess we should really stop that discussion there), this is non-trivial to do from within your application.

    It is not too difficult to reset variables - just write a function that sets all global variables to known values. However, you will not be able to reset the heap, or any states stored inside library functions that you haven't got access to the global/static/heap-allocated variables of, and many other things that you can't "fix up" either.

    You need to exit the application and start over again.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    You need to exit the application and start over again.
    If you're on a UNIX environment, this isn't too difficult. Get back into the main() function somehow (either returning up to it, or longjmp'ing there), chdir() back to wherever you started (if you moved), close all open file descriptors except for stdin, stdout, stderr, then call:

    Code:
    execv(argv[0], argv);
    This also assumes you haven't twiddled anything inside the argv array.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    If you're on a UNIX environment, this isn't too difficult. Get back into the main() function somehow (either returning up to it, or longjmp'ing there), chdir() back to wherever you started (if you moved), close all open file descriptors except for stdin, stdout, stderr, then call:

    Code:
    execv(argv[0], argv);
    This also assumes you haven't twiddled anything inside the argv array.
    Yes, works very well [and we could use a setjmp/longjmp pair to get back to main, for example].

    Unfortunately I happen to know that the OS that sergioms is using is not unix - it is a DOS-like OS for an embedded microcontroller, and it is sufficiently DOS-like to support Turbo C and Turbo C++, so I guess it performs an "exec()" more like a "spawn()" than Unix exec.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    thanks a lot again

    this topic maybe be closed, matsp helping me in other topic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. changing the address of a pointer question..
    By transgalactic2 in forum C Programming
    Replies: 42
    Last Post: 10-16-2008, 09:20 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 04:44 AM
  5. Memory pointers
    By electrolove in forum C Programming
    Replies: 12
    Last Post: 02-05-2003, 05:48 AM