Other HI-TECH Compilers & Products >> 8051 Legacy Compiler

Pages: 1
harrymc



Reged: Oct 04 2004
Posts: 4
Loc: Perth, Western Australia
Sizeof yields 0 (warning)
      #51142 - Thu Apr 17 2008 11:30 PM

Hello

I've struck a little problem porting some code that compiles with SDCC (ANSI C99) to C51 (ANSI C89 ?).

$ c51 -v
HI-TECH 8051 C COMPILER V8.00
running on debian etch

The mm.c test:
-----------------------
#define USB_DT_STRING 0x3

code unsigned char desc_serial_str[] = {
sizeof(desc_serial_str),
USB_DT_STRING,
'1',0,'2',0,'3',0,'4',0,'5',0,'6',0
};

main()
{
}
-----------------------
gives:
mm.c: 4: Sizeof yields 0 (warning)

This compiles and correctly fills element 0 using SDCC; I suspect this is because of variable array size handling of C99.

Examining the mm.hex file shows that element 0 is initialized to zero:
00 03 31 00 32 00 33 00 34 00 35 00 36 00

I'd really like to find a way to have the size calculated by the C51. Some of the arrays are quite large so counting the entries and hard coding the size is a source of (human) error.

I've experimented with a desc_serial_end: label inside the array initialization body and a calculated size:
desc_serial_end - desc_serial_str
(hoping the linker would resolve the size) but the compiler expects a constant and this fails (as does the use of a label anyway).

Using C allows some flexible definition of USB descriptors based on modules compiled and linked for the target device and it replaces less flexible .asm files.

I'd rather not have a script that builds the descriptors if I can avoid it (so maintainers/developers have an easy view of how the descriptors are created).

Is there a C89 (or C51 specific) solution for this ?

All the best
Harry


Post Extras: Print Post   Remind Me!   Notify Moderator  
harrymc



Reged: Oct 04 2004
Posts: 4
Loc: Perth, Western Australia
Re: Sizeof yields 0 (warning) [Re: harrymc]
      #52642 - Sun Apr 27 2008 08:57 AM

Weeeell

I guess I'll have to hard code it since it's stumped 100 or so other readers :-)

I might use an #ifdef in the code to use the sizeof for SDCC and hard code the values (with a warning about changing it if the string is modified) for C51.

Ugly but no obvious alternative.

Harry


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:  ndouglas, jtemples, Dan Henry, Andrew L, mikerj, dave g, meisty, josh stevo 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 1641

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5