How to Restart or Clean all Variable in memory?

This is a discussion on How to Restart or Clean all Variable in memory? within the C Programming forums, part of the General Programming Boards category; Hi,, how to restart (self) application ? I Need Clean all variable allocateds, and start again the program... I Know ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    158

    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
    Posts
    23,014
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Posts
    158
    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, 10:20 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 03:15 PM
  4. Managing shared memory lookups
    By clancyPC in forum Linux Programming
    Replies: 0
    Last Post: 10-08-2003, 05:44 AM
  5. Memory pointers
    By electrolove in forum C Programming
    Replies: 12
    Last Post: 02-05-2003, 05:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21