Is it possible to impliment printf without malloc?

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
tkahn6
Posts: 11
Joined: Sat Jul 11, 2009 5:14 pm

Is it possible to impliment printf without malloc?

Post 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!!
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Re: Is it possible to impliment printf without malloc?

Post 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.
The continuous image of a connected set is connected.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Is it possible to impliment printf without malloc?

Post 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?
Every good solution is obvious once you've found it.
wrschlanger
Posts: 6
Joined: Tue Jul 14, 2009 9:06 am

Re: Is it possible to impliment printf without malloc?

Post 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
Matthew
Member
Member
Posts: 48
Joined: Wed Jul 01, 2009 11:47 am

Re: Is it possible to impliment printf without malloc?

Post 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.
tkahn6
Posts: 11
Joined: Sat Jul 11, 2009 5:14 pm

Re: Is it possible to impliment printf without malloc?

Post 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.
tkahn6
Posts: 11
Joined: Sat Jul 11, 2009 5:14 pm

Re: Is it possible to impliment printf without malloc?

Post 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?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Is it possible to impliment printf without malloc?

Post 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).
Last edited by Solar on Tue Jul 14, 2009 11:02 pm, edited 1 time in total.
Every good solution is obvious once you've found it.
User avatar
imate900
Member
Member
Posts: 80
Joined: Sat Feb 28, 2009 11:43 am

Re: Is it possible to impliment printf without malloc?

Post by imate900 »

Yes it is possibile.
Current work on a OS: SauOS (project homepage: http://code.google.com/p/sauos/)
Image
Post Reply