avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] printf and printf_P suggestions


From: Trampas
Subject: [avr-gcc-list] printf and printf_P suggestions
Date: Tue, 15 Aug 2006 09:18:32 -0400

I was thinking about the printf() function. Here is an idea I used to write
my own printf() on another Harvard processor. Since strings could be stored
in RAM or in flash there as a question as which to use. I know that we have
the printf_P() to aid in this, however if you have:

const char data[]="hello world";

printf(data);

This code is a problem, actually the compiler should catch the error.
However what I did was make it such that in the printf() routine I checked
the address of the format string. Thus if the address was in flash I knew to
call the printf_P() version if it was in RAM then standard printf was used. 

I was wondering if this was possible in the WinAVR port. For example if an
inexperience user wrote:

printf("Hello World"); 

You could have it where "Hello World" is copied to data memory before
execution of the printf, or you could leave "Hello World" in program memory
and save data memory. By modifying the printf routine the one function could
accommodate both methods. I would personally like to have a command line
option to tell the compiler not to copy the strings to data memory in the
above case. 

The reason I personally would like such features is that I am porting a
large amount of code which has:

sprintf(str, "results %d\n",data); 

Thus having the option would save me a lot of data memory and/or time
correcting the sprintf calls to sprintf_P. 

I would be happy if someone has a better suggestion or maybe it has been
implemented. 


Thanks
Trampas





reply via email to

[Prev in Thread] Current Thread [Next in Thread]