[cc65] Fwd: [cc65-devel] strange results with pointers

From: Oliver Schmidt <ol.sc1web.de>
Date: 2013-08-09 21:17:20
Just in case someone on this list is interested ...

---------- Forwarded message ----------
From: Greg King <gregdk@users.sf.net>
Date: Thu, Aug 1, 2013 at 5:45 PM
Subject: Re: [cc65-devel] strange results with pointers
To: cc65 Development <cc65-devel@lists.sourceforge.net>


From: "Chris Cureau"; on Tuesday, July 30, 2013; at 8:54 PM -0400
>

> I've got some code written to translate an "absolute" sector on the c1541
> disk image to a track/sector number.  It works fine using gcc; but, I get
> really weird results with cc65.  In fact, on multiple runs, I haven't
> gotten a consistent run time or result yet.
>
> I'm pretty sure this has to do with the use of pointers; but, I don't see
> anything wrong.  Is there something that I'm missing?  The code is, of
> course, not optimized; but, it works with gcc.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #include "include/types.h"
> #include "include/tsconv.h"
>
> UCHAR ts_array[40] = {
>        // Tracks 0-16 have 21 sectors (0-20)
>        21, 21, 21, 21, 21, 21, 21, 21,
>        21, 21, 21, 21, 21, 21, 21, 21, 21,
>
>        // Tracks 17-23 have 19 sectors (0-18)
>        19, 19, 19, 19, 19, 19, 19,
>
>        // Tracks 24-29 have 18 sectors (0-17)
>        18, 18, 18, 18, 18, 18,
>
>        // Tracks 30-34 have 17 sectors (0-16)
>        17, 17, 17, 17, 17,
>
>        // WARNING - HERE BE NONSTANDARD TRACKS
>        // Tracks 35-39 have 17 sectors (0-16)
>        17, 17, 17, 17, 17
> };
>
> struct ts * tsconv(int absolute) {
>        unsigned char *ptr = ts_array;
>        int temp = 0;
>
>        struct ts * rec = malloc(sizeof (struct ts));
>        rec->track = 0;
>        rec->sector = 0;
>
>        while ((absolute - (temp + *ptr)) > 0) {
>                rec->track++;
>                temp += *(ptr++);
>        }
>        rec->sector = absolute - temp;
>        return rec;
> }

It is a cc65 bug.  The expression "temp + *ptr" is promoted to unsigned int
instead of signed int.

_______________________________________________________
cc65-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cc65-devel
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Fri Aug 9 21:18:50 2013

This archive was generated by hypermail 2.1.8 : 2013-08-09 21:18:52 CEST