All variables changed any ideas?
All variables changed any ideas?
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?
Many Apologies. the problem is with my printf code, i have no idea how though any help would be appreciated.
My printf code:
Thank you in advanced
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--;
}
}
Solved
I ported the printf statements from linux 0.01
Why are you casting format to pointer to pointer to char?
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....
Code: Select all
char **arg = (char **) &format;
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....