Page 1 of 1

All variables changed any ideas?

Posted: Tue Apr 22, 2008 10:50 am
by xarnze
All of the variables in my code have changed to 1053345 if they are int or i` if they are a char. i'm not sure what could be causing this although i think it may be a linker problem. anyone got any ideas?

Posted: Tue Apr 22, 2008 11:03 am
by Combuster
Try fixing your print function. If that doesn't work, try the memory manager.

Posted: Tue Apr 22, 2008 11:16 am
by xarnze
This problem has only recently occurred and my print function is working as static strings remain unchanged, whenever the variable is accessed by anything its value is as above. it happens even before the memory manager is enabled.

Posted: Tue Apr 22, 2008 11:39 am
by xarnze
Many Apologies. the problem is with my printf code, i have no idea how though any help would be appreciated.

My printf code:

Code: Select all

int __stack_chk_fail;

void printf(const char *format, ...)
{
  char **arg = (char **) &format;
  int c;
  char buf[20];

  arg++;

  while((c = *format++) != 0)
    {
      if(c!='%')
	putch(c);
      else
	{
	  char *p;

	  c = *format++;
	  switch(c)
	    {
	    case 'd':
	    case 'u':
	    case 'x':
	      itoa(buf, c, *((int *) arg++));
	      p = buf;
	      goto string;
	      break;

	    case 's':
	      p=*arg++;
	      if(!p)
		p="(NULL)";

	    string:
	      while(*p)
		putch(*p++);
	      break;

	    default:
	      putch((*((int *) arg++)));
	      break;
	    }
	 }
     }
}

static void itoa(char *buf, int base, int d)
  {
    char *p = buf;
    char *p1, *p2;
    unsigned long ud = d;
    int divisor = 10;

    // if %d and is '-' add -
    if(base == 'd' && d < 0)
      {
	*p++ = '-';
	buf++;
	ud = -d;
      }
    else if(base == 'x')
      divisor = 16;

    //divid UD by divisor until UD == 0
    do
      {
	int remainder = ud % divisor;
	*p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
      }
    while(ud /= divisor);

    //terminater the buf.
    *p = 0;

    //reverse the buf
    p1 = buf;
    p2 = p - 1;
    while(p1<p2)
      {
	char tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
	p1++;
	p2--;
      }
  }
Thank you in advanced

Solved

Posted: Tue Apr 22, 2008 2:23 pm
by xarnze
I ported the printf statements from linux 0.01

Posted: Tue Apr 22, 2008 2:32 pm
by -m32
Why are you casting format to pointer to pointer to char?

Code: Select all

char **arg = (char **) &format;
If you are trying to read the arguments from the variable parameters list, this won't work, at least, not well.

Then in your call to itoa you're doing *((int *) arg++) - Casting the **arg pointer to int then dereferencing garbage.

That's what it looks like anyway. I'd suggest looking at stdio.h/c for GNU stdlib.

PS: Using 'goto' in C is very poor style....

Posted: Tue Apr 22, 2008 3:32 pm
by lukem95
its a direct port from linux, it works fine for most people. i use a modified version in my OS...

i can't see why this would be the problem

Posted: Tue Apr 22, 2008 3:46 pm
by xarnze
The above code was not a port from linux but i found that the printf code was the problem and replaced it with the printf code from linux, it's now modified so that it works with my original syntax but otherwise a direct port.
thanks for all the help.