Page 1 of 1
Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 10:38 am
by tkahn6
Let me rephrase that: Is it possible to
elegantly implement printf without malloc?
I have an idea to do it with walking up the va_list and computing how many characters each variable's value would need to display, and then creating a character array with that number. If I'm making no sense, heres an example:
Code: Select all
char newstr[kstrlen(str) + knumlen_baseten(integer) + 1]; //1 for the \0
kstrcpy(newstr, str);
kdrawstr(newstr, 0, 0, BLACK, WHITE);
This is the code for knumlen_baseten
Code: Select all
uint8 knumlen_baseten(uint32 number)
{
uint8 number_of_digits = 0;
while ((number /= 10) > 0)
{
number_of_digits++;
}
return (number_of_digits+1);
}
Does this look dumb? (I plan to do something like *newstr++ = *str++ and when *str == '%', it looks ahead at the next character, and, say if its a 'd', it'll strcopy(newstr, kwhateverjustconvertanumberintoastringbutforgoethenullterminator()))
Thanks!!
Re: Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 11:09 am
by mathematician
In C when you pass a string you are really passing a pointer, so, unless you have an exceedingly small stack, I don't see the problem.
Re: Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 11:11 am
by Solar
tkahn6 wrote:Let me rephrase that: Is it possible to elegantly implement printf without malloc?
Is it possible to
elegantly implement printf
with malloc?
I'm a bit mushy in the head right now after a day's worth of debugging, but I can't really fathom what you'd need a malloc()'ed array for?
Your stdout is a stream. A stream already has a buffer, no?
Re: Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 11:15 am
by wrschlanger
It is very possible to implement printf without malloc, if you use the stack for storage space instead. Remember, even 64-bit numbers in decimal are no more than say 256 characters in length, so you can easily do this:
char output[256];
You can then write to output and then easily print out characters in the array - backwords - no need for malloc!
In fact, most standard implementations of printf don't use malloc, but use the stack instead.
For more details, see the printf implemention here:
http://geezer.osdevbrasil.net/osd/index.htm
For an example of a 64bit OS I developed that uses it, see here:
http://vm64dec.googlecode.com/files/options64-0.02.zip
Cheers!
Willow
Re: Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 11:45 am
by Matthew
I put together a printf implementation handling padding and field-width with some loops and switch statements last week w/o using malloc. You don't need any additional space because the %-directives tell you which type to expect to find on the stack (of course it's not type-safe but hey). It's not hard and there's plenty of examples online. And you don't need to print numbers in reverse to a buffer either -- you can output them starting from most-significant-digit directly.
Re: Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 11:52 am
by tkahn6
Matthew wrote:I put together a printf implementation handling padding and field-width with some loops and switch statements last week w/o using malloc. You don't need any additional space because the %-directives tell you which type to expect to find on the stack (of course it's not type-safe but hey). It's not hard and there's plenty of examples online. And you don't need to print numbers in reverse to a buffer either -- you can output them starting from most-significant-digit directly.
Yeah I feel dumb. I had this idea that I needed to read the characters into a buffer. My idea using malloc would be create a linked list of the different variables as they come and then walk across it........ now that i think about it that doesn't make much sense.
Re: Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 12:03 pm
by tkahn6
wrschlanger wrote:It is very possible to implement printf without malloc, if you use the stack for storage space instead. Remember, even 64-bit numbers in decimal are no more than say 256 characters in length, so you can easily do this:
char output[256];
You can then write to output and then easily print out characters in the array - backwords - no need for malloc!
In fact, most standard implementations of printf don't use malloc, but use the stack instead.
For more details, see the printf implemention here:
http://geezer.osdevbrasil.net/osd/index.htm
For an example of a 64bit OS I developed that uses it, see here:
http://vm64dec.googlecode.com/files/options64-0.02.zip
Cheers!
Willow
Thanks! Your code is very easy to understand - very well commented and logical. When you refer to 'the stack' is this the call stack? In K&R they have you implement a 'stack' that is simply a large array. Can you link or post a simple example of what you mean?
Re: Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 1:51 pm
by Solar
Local variables that aren't malloc'ed reside on the stack. (The one with the stack segment.)
My own printf() implementation doesn't even have that, though. If you look
here and
here, you'll see that I set up a struct, containing various informations like numbers of characters printed (for snprintf() etc.), flags of the current conversion specifier, width and padding required etc. - and handle all variable-to-output conversions by recursion. Output is not buffered by printf(), but by the stream in question.
Note that I dropped that code like a hot potato once it worked reliably, for the time being. It's uglier than my usual code... but I think the concept is sound (and more ellegant than malloc()'ing in any case).
Re: Is it possible to impliment printf without malloc?
Posted: Tue Jul 14, 2009 2:50 pm
by imate900
Yes it is possibile.