Re: [cc65] sizeof("string") gives wrong results

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2002-05-16 18:01:15


Hi!

On Thu, May 16, 2002 at 05:38:18PM +0200, Spiro Trikaliotis wrote:
> > the following program returns 3 as sizeof the string. It should return
> > 4 (count the terminating 0 byte).
>
> no, this is correct ANSI C. If you want to count the terminating NULL
> byte, you have to add one.

I cannot find anything in the standard that supports this. And gcc does indeed
include the trailing zero, so I would say, Christian is right here, it's a bug.

Fortunately, the fix is rather easy. Here is the patch:

-----------------------------------------------------------------------------
Index: expr.c
===================================================================
RCS file: /usr/local/cvsroot/uz/c64/cc65/src/cc65/expr.c,v
retrieving revision 1.55
diff -r1.55 expr.c
991c991
< 	lval->Type  = GetCharArrayType (strlen (GetLiteral (CurTok.IVal)));
---
> 	lval->Type  = GetCharArrayType (GetLiteralPoolOffs () - CurTok.IVal);
-----------------------------------------------------------------------------

This will also fix sizeof() for strings with embedded NULs.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2002-05-16 18:01:16 CEST