A little problem

suppose a function f1 is running and there on getting a condition true, the program should jump to the starting of main function,then how will I do this.remember main called f2() and f2() called f1().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
f1()
{
    code...
    if( condition 1 is true)
    {
    jump to starting of main and never return here
    }
    code....
}

f2()
{
    code....
    f1();
    code...
}

main()
{
    code...
    f2();
    code;
}
Uh you can't? Not without some nasty hack. Why would you want to anyway? That's terrible design.
How about unrolling the functions and using a do {} while?
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    do {
        code...
        // contents of f2():
        code....
        // contents of f1():
        code...
    while( condition 1 is true);
    code....
    code...
    code;
}
@ResidentBiscuit : I'm design server.In that if the error occurred the program unbound the socket cleans WSA data and then returns to beginning of main where a function to bind the socket is called.

@moorecm: Thanx for the cooperation but I resolved the problem by using setjmp.h. I read about it right now over here

http://www.cplusplus.com/reference/csetjmp/
Not saying this is a grand idea, but:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdexcept>
#include <iostream>

struct Destructo
{
    Destructo() { std::cout << "Destructo created!\n" ; }
    ~Destructo() { std::cout << "Destructo destroyed!\n" ; }
};


void f2()
{
    std::cout << "Entered f2\n"  ;
    
    throw std::exception("My return exception") ;

    std::cout << "Exiting f2\n" ;
}

void f1()
{
    std::cout << "Entered f1!\n" ;

    Destructo theGreatDestructo ;

    f2() ;

    std::cout << "Exiting f1!\n" ;
}


int main()
{
    std::cout << "Entered main!\n" ;

    try 
    {
        f1() ;
        std::cout << "Normal return from f1\n" ;
    }
    catch(std::exception& ex)
    {
        std::cout << "Returned to main by exception.\n" ;
    }

    std::cout << "Exiting main!\n" ;
}

I can think of some hacks to make it work, but nothing anyone would call "elegant", but it looks as if you found a solution now. Sorry we couldn't be more help!
What about exceptions ?
The solution I offered was elegant. I'd recommend it over doing anything fancy.
The solution I offered was elegant.


I don't think I'd describe "put everything in main" as elegant.
> suppose a function f1 is running and there on getting a condition true, (A)
> the program should jump to the starting of main function (B)

In the execution path between A and B, if and only if

a. there are no local objects (including anonymous temporaries) which have non-trivial destructors
b. there are no try/catch constructs
c. no exceptions would be thrown

then setjmp() / std::longjmp()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <csetjmp>
#include <cstdio>

std::jmp_buf setjmp_environment ;

void f1( int a )
{
    printf( "enter f1(%d)\n", a ) ;
    if( a == 0 )
    {
        puts( "jump from f1()" ) ;
        std::longjmp( setjmp_environment, 1 ) ;
    }
    puts( "continue with f1()" ) ;
}

void f2( int a )
{
    printf( "enter f2(%d)\n", a ) ;
    printf( "call f1(%d)\n", a ) ;
    f1(a) ;
    puts( "continue with f2()" ) ;
}

int main()
{
    puts( "enter main() and setjmp" ) ;
    int value = setjmp( setjmp_environment ) ; // no std::, may be a macro
    puts( "**** at the beginning of main() ****" ) ;
    printf( "value == %d\n", value ) ;
    puts( "continuing with main() " ) ;
    printf( "call f2(%d)\n", value ) ;
    f2( value ) ;
    puts( "return from f2()" ) ;
    puts( "exit main()" ) ;
}

I don't think I'd describe "put everything in main" as elegant.


Your inference is not the same as the statement's intent. Restructuring is necessary, as the requirement to "magically jump" in this context is evidence that the program is, well, already broken.

Correctness, simplicity, and clarity come FIRST. These principles are, by definition, elegant.
Topic archived. No new replies allowed.