All variables changed any ideas?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
xarnze
Posts: 19
Joined: Thu Nov 02, 2006 4:45 pm
Location: United Kingdom
Contact:

All variables changed any ideas?

Post 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?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Try fixing your print function. If that doesn't work, try the memory manager.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
xarnze
Posts: 19
Joined: Thu Nov 02, 2006 4:45 pm
Location: United Kingdom
Contact:

Post 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.
xarnze
Posts: 19
Joined: Thu Nov 02, 2006 4:45 pm
Location: United Kingdom
Contact:

Post 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
xarnze
Posts: 19
Joined: Thu Nov 02, 2006 4:45 pm
Location: United Kingdom
Contact:

Solved

Post by xarnze »

I ported the printf statements from linux 0.01
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Post 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....
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post 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
~ Lukem95 [ Cake ]
Release: 0.08b
Image
xarnze
Posts: 19
Joined: Thu Nov 02, 2006 4:45 pm
Location: United Kingdom
Contact:

Post 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.
Post Reply