#160 memory space during pointer cast seems lost

open
nobody
None
5
2006-08-30
2006-08-30
No

The information of the memory space is not used in
function f of the code below.

So the code is either large in case of f() or
less portable in case of g().

----------8<------------------------------------
#include <8051.h>

code unsigned char table[8] = {
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80 };

idata unsigned long id;

/* about 70 (+8) byte */
void f (void)
{
unsigned char i = 0;

do
{
if (P0_1)
{
/* information that &id is an address in idata
memory space is not used */
((unsigned char *) &id)[i / 8] |= table[i % 8];
}
}
while( ++i != 32 );
}

/* about 30 (+8) byte */
void g (void)
{
unsigned char i = 0;

do
{
if (P0_1)
{
/* cast to idata pointer makes code
less portable */
((unsigned char idata *) &id)[i / 8] |= table[i
% 8];
}
}
while( ++i != 32 );
}

Discussion

  • Frieder Ferlemann

    Logged In: YES
    user_id=589052

    This example is less clumsy than the one in the original report:

    -------8<-------------------------
    idata unsigned long id;

    void main (void)
    {
    unsigned char *p = &id;

    /* call of __gptrput could be avoided */
    p[2] = 0xab;
    }

     
  • Maarten Brock

    Maarten Brock - 2011-03-28

    For quite some time now SDCC generates perfect code for f() and g(). Only not yet for the "less clumsy" main() example.

     

Log in to post a comment.