Go Down

Topic: Is this legal / safe programming? (Read 434 times) previous topic - next topic

Krupski

Hi all,

Take a look at this code:

Code: [Select]

void Noritake_VFD_GUU100::setFont(const uint8_t *data)
{
        if (data) { // got a pointer, get it's data
                _fontWidth = pgm_read_byte(data + 0);
                _fontHeight = pgm_read_byte(data + 1);
                _fontHGap = pgm_read_byte(data + 2);
                _fontVGap = pgm_read_byte(data + 3);
                _firstChar = pgm_read_byte(data + 4);
                font = (data + 5);
        } else { // default requested, loop back and load it
                setFont(hitachi_hd44780u_a0_5x8_font);
        }
}


Notice that if there is no value to "data" (i.e. if called with setFont(0); the function re-enters itself again with a default parameter.

What I want it to do is this:

* If not called, do nothing (load no font)
* If called with (0), load a built in "default" font
* If called with an actual pointer to a font table, load that font instead

The code works in that it fulfils the three cases above, but my question is, is it wrong for the code to call itself? Will it cause a memory leak or screw up the stack or make the sun stop shining?

Input will be appreciated. Thanks.

-- Roger
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

billroy

What you have there works fine, but you can save the one-step recursion with something like this at the top to override "data" if it's not set:

Code: [Select]

    if (!data) data = hitachi_hd44780u_a0_5x8_font;


-br

majenko

That is called recursion.

It has often been said "To iterate is human, to recurse, divine.".

Recursion is fine, as long as you don't go too deep.  There is only so much stack space.

Krupski


What you have there works fine, but you can save the one-step recursion with something like this at the top to override "data" if it's not set:

Code: [Select]

    if (!data) data = hitachi_hd44780u_a0_5x8_font;


-br



Yes, that's a lot cleaner. I like it. Thanks!
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

MarkT


That is called recursion.

It has often been said "To iterate is human, to recurse, divine.".

Recursion is fine, as long as you don't go too deep.  There is only so much stack space.


Well its a tail-recursive call so a good compiler will re-use the same stack frame for this.
[ I won't respond to messages, use the forum please ]

majenko



That is called recursion.

It has often been said "To iterate is human, to recurse, divine.".

Recursion is fine, as long as you don't go too deep.  There is only so much stack space.


Well its a tail-recursive call so a good compiler will re-use the same stack frame for this.

I have often wondered what a compiler would make of recursion using a forced inline function.

I haven't dared try in case it generates a black hole and the entire universe gets sucked into it...

holmes4

You'll never understand recursion until you have blown your stack  ]:)

Mark

WizenedEE




That is called recursion.

It has often been said "To iterate is human, to recurse, divine.".

Recursion is fine, as long as you don't go too deep.  There is only so much stack space.


Well its a tail-recursive call so a good compiler will re-use the same stack frame for this.

I have often wondered what a compiler would make of recursion using a forced inline function.

I haven't dared try in case it generates a black hole and the entire universe gets sucked into it...

One of three things:
Changes it into a loop (likely, if it can)
Gives an error (unlikely)
Doesn't inline it (very likely)

Go Up