[cc65] Think I found a cc65 bug

Date view Thread view Subject view

From: MagerValp (MagerValp_at_cling.gu.se)
Date: 2002-12-03 18:34:05


cc65 results:
NewMenu returns: 1953
NewMenu()
menu: 1953
->visible: 0
->numitems: 0
->current: 0
->title: 0
->firstitem: 0
SetMenuTitle: 1953
title: 1785
->title: 1785
SetMenuTitle()
->visible: 0
->numitems: 0
->current: 0
->title: 1785
->firstitem: 0
Firstitem is NULL
item malloced at 1959
Firstitem is 1959
Firstitem is still 1959
after item->next firstitem is 59     <- hey now
Firstitem is still 59
Firstitem is still 59
Firstitem is still 59
Firstitem is still 59


gcc results:

NewMenu returns: a0100e0
NewMenu()
menu: a0100e0
->visible: 0
->numitems: 0
->current: 0
->title: 0
->firstitem: 0
SetMenuTitle: a0100e0
title: 4014e6
->title: 4014e6
SetMenuTitle()
menu: a0100e0
->visible: 0
->numitems: 0
->current: 0
->title: 4014e6
->firstitem: 0
Firstitem is NULL
item malloced at a0104f8
Firstitem is a0104f8
Firstitem is still a0104f8
after item->next firstitem is a0104f8
Firstitem is still a0104f8
Firstitem is still a0104f8
Firstitem is still a0104f8
Firstitem is still a0104f8


test code:

#include <stdlib.h>
#include <stdio.h>



typedef enum itemstates {
  ISTATE_NORMAL,
  ISTATE_INACTIVE
} ItemState;

typedef struct item {
  struct item *next;
  unsigned char id;
  unsigned char *name;
  ItemState state;
} Item;

typedef struct menu {
  unsigned char visible;
  unsigned char numitems;
  unsigned char current;
  unsigned char *title;
  Item *firstitem;
} Menu;


void DebugMenu(Menu *menu, unsigned char *s);

Menu *NewMenu(void);

void SetMenuTitle(Menu *menu, unsigned char *title);

char AddMenuItem(Menu *menu, unsigned char id, ItemState state, unsigned char *name);


void DebugMenu(Menu *menu, unsigned char *s) {
  printf("%s\r\n", s);
  printf("menu: %x\r\n", menu);
  printf("->visible: %d\r\n", menu->visible);
  printf("->numitems: %d\r\n", menu->numitems);
  printf("->current: %d\r\n", menu->current);
  printf("->title: %x\r\n", menu->title);
  printf("->firstitem: %x\r\n", menu->firstitem);
}


Menu *NewMenu(void) {
  Menu *menu;

  if ((menu = malloc(sizeof(menu))) == NULL) {
    return(NULL);
  }
  menu->visible = 0;
  menu->numitems = 0;
  menu->current = 0;
  menu->title = NULL;
  menu->firstitem = NULL;
  printf("NewMenu returns: %x\r\n", menu);
  return(menu);
}


void SetMenuTitle(Menu *menu, unsigned char *title) {
  printf("SetMenuTitle: %x\r\n", menu);
  printf("title: %x\r\n", title);
  menu->title = title;
  printf("->title: %x\r\n", menu->title);
}


char AddMenuItem(Menu *menu, unsigned char id, ItemState state, unsigned char *name) {
  Item *item;

  if (menu->firstitem == NULL) {
    printf("Firstitem is NULL\r\n");
    if ((menu->firstitem = malloc(sizeof(item))) == NULL) {
      return(-1);
    }
    item = menu->firstitem;
    printf("item malloced at %x\r\n", item);
    printf("Firstitem is %x\r\n", menu->firstitem);
  } else {
    printf("Firstitem is %x, finding last\r\n", menu->firstitem);
    item = menu->firstitem;
    while (item->next) {
      item = item->next;
    }
    printf("Lastitem is %x\r\n", item);
    if ((item->next = malloc(sizeof(item))) == NULL) {
      return(-1);
    }
    printf("item malloced at %x\r\n", item);
    item = item->next;
  }
  printf("Firstitem is still %x\r\n", menu->firstitem);
  item->next = NULL;                                              <- something's wrong here
  printf("after item->next firstitem is %x\r\n", menu->firstitem);
  item->id = id;
  printf("Firstitem is still %x\r\n", menu->firstitem);
  item->name = name;
  printf("Firstitem is still %x\r\n", menu->firstitem);
  item->state = state;
  printf("Firstitem is still %x\r\n", menu->firstitem);
  ++(menu->numitems);
  printf("Firstitem is still %x\r\n", menu->firstitem);
  return(0);
}

void main(void) {
  Menu *menu;

  menu = NewMenu();
  DebugMenu(menu, "NewMenu()");
  SetMenuTitle(menu, "Main Menu");
  DebugMenu(menu, "SetMenuTitle()");
  AddMenuItem(menu, 13, ISTATE_NORMAL, "File Menu...");
}

-- 
    ___          .     .  .         .       . +  .         .      o   
  _|___|_   +   .  +     .     +         .  Per Olofsson, arkadspelare
    o-o    .      .     .   o         +          MagerValp_at_cling.gu.se
     -       +            +    .     http://www.cling.gu.se/~cl3polof/
----------------------------------------------------------------------
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-12-03 18:34:52 CET