3

I am working on an assignment for an embedded software course, but I'm having the strangest problem.

Using the code below:

void decidePotato(float held)
{
    printf("Deciding Potato, held for %f seconds \n", held);
    if (held >= 1.99)
    {
        printf("Held for more than 1.99s \n", held);
        returnPotato();
    }
    printf("I evaluated the if statement above \n");

}

I get the following output:

Deciding Potato, held for 0.010000 seconds

I dont even see the "I evaluated the if statement above" message, so the program somehow got stuck evaluating that if statement. And it remains stuck until I reprogram the board How is that even possible?

  • 2
    What's in `returnPotato`? – ouah Jul 15 '13 at 21:31
  • 1
    What does `returnPotato` contain? – jsalonen Jul 15 '13 at 21:31
  • I'm guessing that more stuff was printed but is buffered and you don't see it. – Hot Licks Jul 15 '13 at 21:34
  • 1
    Simply copying your function in a c++ program doesn't get me any error. Can you post a [SSCCE](http://sscce.org/) which reproduces the problem? – Sam I am says Reinstate Monica Jul 15 '13 at 21:37
  • @SamIam: C#? The OP is working in C. – RichieHindle Jul 15 '13 at 21:39
  • @RichieHindle wires got crossed – Sam I am says Reinstate Monica Jul 15 '13 at 21:40
  • It's hard to know what's happening without knowing what board you're programming and also how the rest of your program is constructed. What other things are going on when this function is called? Is the C syntax exactly what you need when programming the board? Are you using too much memory on your board with your code? I'm asking because I don't see anything wrong with your code. Either it's very subtle, or the error is happening somewhere else. – John Jul 15 '13 at 21:48
  • 3
    Try peppering the code with `fflush(stdout)`. Sometimes messages get blocked up in the pipeline (despite the often-reasonable assumption that line buffering should be enabled) and lead to unclear behaviour. Don't forget to remove them once you understand the problem -- they can be terribly inefficient. – sh1 Jul 15 '13 at 21:52

3 Answers3

2

I suggest to put a fflush() at the end of your function: even if with the new line you should force printing, it might be that your compiler has a "strange" implementation...

By the way: are you redirecting your output to a file? Because in that case this could apply.

Anyway, as Scotty Bauer noticed, you have to correct the printf within the if block: you probably also got a compiler warning for that.

Note from fflush manual:

If the stream argument is NULL, fflush() flushes all open output streams.

Normally you would do fflush(stdout).

Community
  • 1
  • 1
Antonio
  • 17,405
  • 10
  • 78
  • 178
0

If your program is truly stuck, I would suspect that you overflowed the stack by calling printf(). Then your program tried jumping to undefined memory or dereferencing a junk pointer and took an exception, and it is locked in an infinite loop in the default exception handler. printf is a luxury in the embedded world. You need a big enough stack to handle it, especially you are writing to a complicated device driver or the driver is using up too much memory.

Change the stack size to the maximum you have available and see if the program changes.

(Having extra args to printf() that are not used is not a problem. It's not a good idea, but they are just ignored.

Mark Lakata
  • 18,024
  • 5
  • 88
  • 112
  • Some compilers let you invoke a "tiny" `printf` that does not support `float`. Using `float` in a `printf `is very expensive. – Mark Lakata Jul 15 '13 at 22:26
-1

It's impossible to know without looking at returnPotato(). It could be that you're in returnPotato and getting blocked in there, and the printf didn't flush.

Please also note, you have

printf("Held for more than 1.99s \n", held);

And you don't have a %f in your format string to place held into.

Scotty Bauer
  • 1,255
  • 9
  • 14