Pages: [1]   Go Down
Author Topic: Is this legal / safe programming?  (Read 398 times)
0 Members and 1 Guest are viewing this topic.
Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 799
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

Take a look at this code:

Code:
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
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    if (!data) data = hitachi_hd44780u_a0_5x8_font;

-br
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 799
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    if (!data) data = hitachi_hd44780u_a0_5x8_font;

-br


Yes, that's a lot cleaner. I like it. Thanks!
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

0
Offline Offline
Shannon Member
****
Karma: 206
Posts: 12155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

[ I won't respond to messages, use the forum please ]

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2296
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You'll never understand recursion until you have blown your stack  smiley-evil

Mark
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Pages: [1]   Go Up
Jump to: