3

I have a main function that runs a few functions during initialization and then runs a while loop that waits for commands from the UART.

When I see a specific command (let's say reset), I call a function that returns a value. I want to do the following things:

  1. Save the returned value
  2. Start the main function again with the returned value. The returned value is required during initialization of the functions in main.

I am newbie in C and I am not able to figure out a way save variable value in main.

2
  • Most likely you'll have to write the value to a file (or nonvolatile memory) and reset the processor. When main starts it can read from this memory location to get the value. Commented Aug 8, 2014 at 23:52
  • What platform? Embedded demo board? Do you have access to external nonvolatile memory? Can you partition microcontroller nonvolatile memory? Commented Aug 8, 2014 at 23:56

3 Answers 3

4

The way I understand things, you essentially have the following setup:

int main(int argc, char *argv[]) {
    int value = something_from_last_reset;
    perform_initialization(value);
    while(1) {
        int next_command = wait_for_command();
        if(next_command == RESET_COMMAND) {
            value = get_value();
            // somehow restart main() with this new value
        }
    }
    return 0;
}

Here's one approach you could take:

// global
int value = some_initial_value;

void event_loop() {
    while(1) {
        int next_command = wait_for_command();
        if(next_command == RESET_COMMAND) {
            value = get_value();
            return; // break out of the function call
        }
    }
}

int main(int argc, char *argv[]) {
    while(1) {
        perform_initialization(value);
        event_loop();
    }
    return 0;
}

This essentially lets you "escape" from the event loop and perform the initialization all over again.

Sign up to request clarification or add additional context in comments.

Comments

2

just wrap your main into infinity-loop.

int main(void)
{
    int init_val = 0;
    while (1)
    {
        // your code ...
        init_val = some_function();
    }
}

5 Comments

How does OP reset the processor (or restart main) and set up the system with the new init_val?
@FiddlingBits resetting the processor is your idea, not part of the question. This is in fact the standard solution. Functionally it is the same as your "goto" only cleaner in presentation.
This is not a solution because this is what the OP mentioned he was going to do. He needs to reinit the system from scratch.
@FiddlingBits I don't think the question is how to do that. The question is why the hell would someone want to do that when you have resources like while loop and the ability to separate your code in functions. OP is obviously tunnel-visioned in this nonsense approach choice when there are so many options.
@FiddlingBits initing, main-code, deiniting are in the while(1) loop - I definitely comment // your code....
2

In theory this is possible, but it kind of breaks paradigms, and repetitively calling a function without ever letting it finish and return will quickly fill up your call stack, unless you take measures to unwind it behind the compiler's back.

A more common solution would be to write your main() function as one giant infinite while {1} loop. You can do all of your operation in an innner loop or whatever, and have clauses such that if you get your desired new value you can fall through to the bottom and loop back, effectively re-running the body of main with the new state.

4 Comments

No, it's impossible - standard forbids to call main recursively.
@ikh that's untrue. Try it and you will see. But I will grant that it is unwise to do so, and unlimited, uncompensated recursion will crash the stack. We've both suggested the same alternative.
+1 This is the better solution. An example of your nested loops idea would be a good addition.
@ChrisStratton Oh, I'm sorry... In C, that's not forbidden. I was just confused because that's forbidden and causes undefined behavior in C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.