Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
41 user(s) are online (23 user(s) are browsing Forums)

Members: 1
Guests: 40

hlt, more...

Support us!

Headlines

 
  Register To Post  

Break en entire program from inside a function.
Quite a regular
Quite a regular


See User information
Hi All,

I'm looking my C/C++ documentation ... but didn't found this functionality.

Imagine this program :

void main void ){
   
MyFunction();
 }

void MyFunctionvoid ){
  
//* I WANT TO BREAK/END PROGRAM FROM HERE *//
 
}


I think that code say all :p
Is there a way to stop entirely a program from inside a function without going back to main ?

Thank you.

Kindest Regards,
Fred

All we have to decide is what to do with the time that is given to us.
Go to top
Re: Break en entire program from inside a function.
Just popping in
Just popping in


See User information
@freddix

Learn to code properly?

What you ask is very bad practice.

Go to top
Re: Break en entire program from inside a function.
Just popping in
Just popping in


See User information
@freddix

The reason it is not possible is that there is a fair amount of shut-down code that executes as soon as main is done. Likewise the memory used by the function and the stack must be deallocated. For that reason, there must be only one exit point in the program: the end of main().

Go to top
Anonymous
Re: Break en entire program from inside a function.
exit( ) abort() and nice little calls like that.. ...if you wanted to /pause/ it just do a cin

In C++ just throw an exception that throws back to main.

void MyFunction();

int main( int argc, char * argv[ ] )
{
try
{
DoSomething();
DoSomethingElse();
MyFunction();
} catch ( ... )
{
cout << "Help! I'm going to die!" << endl;
}
}

void MyFunction( )
{
throw // your exception here;
}

Go to top
Re: Break en entire program from inside a function.
Quite a regular
Quite a regular


See User information
@Samurai_Crow & Fab:
I develop an engine so, if user enter an erroneous value in a function, the program must stop giving the error message in CLI ... so, it's not in this case a bad practise but a need for that precise kind of development.

@DaveP :
Thank you for this informations.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: Break en entire program from inside a function.
Just popping in
Just popping in


See User information
Using abort() in the Amiga software is not recommended and I would not recommend using exit() either if native API calls are used (windowed applications mostly).

Go to top
Re: Break en entire program from inside a function.
Just popping in
Just popping in


See User information
@freddix

Quote:

I develop an engine so, if user enter an erroneous value in a function, the program must stop giving the error message in CLI ... so, it's not in this case a bad practise but a need for that precise kind of development.


If you start allocating resources here and there you will find that it is.
What's wrong with using return codes ?

Go to top
Anonymous
Re: Break en entire program from inside a function.
Meanwhile, back on planet do what you fancy. Just use an allocator ADT that automatically deallocates as the registered function on atexit. Or in C++ use smartpointers.

(question to self, does atexit get called... )

Go to top
Anonymous
Re: Break en entire program from inside a function.
@DaveP

Does here... on cygwin, windows and linux. Each atexit function gets called in ... reverse order. Can avoid an adt...

Go to top
Re: Break en entire program from inside a function.
Home away from home
Home away from home


See User information
@freddix

exit() will do this for you, it quits the program exactly as if you'd come to end of main.

Should you require any additional cleanup, ie freeing non Clibrary resources such as amigaos libraries opened by the directly program, memory allocated directly with exec.library then create a cleanup function and pass it to atexit(). It will then be called when you call exit() or when you fall through main().




http://www.cplusplus.com/reference/clibrary/cstdlib/exit/

http://www.cplusplus.com/reference/clibrary/cstdlib/atexit/


You should avoid abort() though as this does no cleanup and should only be called when something has gone very badly wrong.

http://www.cplusplus.com/reference/clibrary/cstdlib/abort/

Go to top
Re: Break en entire program from inside a function.
Home away from home
Home away from home


See User information
@DaveP

The only potential problem with atexit() is that there is a limit to the number of functions you can register, the specification specifies at least 32. No ideas what the practical limit for amigaos clibraries is ....


It's always called on exit unless you call abort()

Go to top
Anonymous
Re: Break en entire program from inside a function.
@broadblues

Thats not a problem, thats fun for all the family.

Go to top
Re: Break en entire program from inside a function.
Quite a regular
Quite a regular


See User information
@Centaurz:
In fact you're totally wrong (for my case)

On my system, each plugin has its own constructor() and destructor() function.
The engine setup call all constructors and a program end automatically call all destructors and then release all memory/media created for each plugins.

That mean that creating a "break" command for my langage, will result in a command that'll call all destructors and then release all memories :p
It's a safe system.

The objective is to create a safe secure exit to prevent crash from when user enter corrupted/erraneour/invalid data in a command of the engine.

Of course the break system will have its own security check to avoid infinite loop if finding error when calling destructors commands ;)

@DaveP : no need ;)
@Broadblues : read upper ;)

I think I'm more clear in what I do and why I do that ?

Bye,
Fred


Edited by freddix on 2009/7/25 23:04:30
All we have to decide is what to do with the time that is given to us.
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project