开发者

Internals of printf

开发者 https://www.devze.com 2023-03-28 14:13 出处:网络
what is the limit of memory that printf can utilize for storing its computed arguments? What is the general memory size available for any command (with variable no. of arguments), to store its argumen

what is the limit of memory that printf can utilize for storing its computed arguments? What is the general memory size available for any command (with variable no. of arguments), to store its arguments?

Example code:

#include <stdio.h开发者_JS百科>

#include <stdlib.h>


int main(int argc, char *argv[])

{

//by default the decimal is considered as double

float a = 0.9;

//long double b = (long double)23455556668989898988988998898999.99 ;

long double b = 5.32e-5;

double  z = 6789999000000.8999;


//b = (long double)1.99999999;

printf("float %f, \n double %lf,\n long double %Lf\n\n\n", b, b, b);

printf("simple:  long double %Lf, double %lf, float %f\n\n\n", b,b,b);

printf(" sumi: float %f, double %lf, long double %Lf\n\n\n", z, z, z);

printf("test2 for le/lg/lf: dbl f %Lf, double g %Lg, double e %Le\n\n\n", b, b, b);  

  system("PAUSE");  

  return 0;

}


There is no specific limit. The biggest danger in an excessive #/arguments is in accidentally overflowing the stack.

What a great question for "stackoverflow.com" ;-)


The minimum memory requirements for printf to support printing any of the types you can pass to it is about 6k of stack space, assuming 15-bit exponent on long double. For any non-floating-point type, you could get by with 200 bytes or less.

Of course most real-world implementations are not maximally-efficient here. glibc at least performs malloc as part of printf, at least for some formats, and thus has unpredictable failure cases. As far as I can tell this is just lazy coding... No idea what MS's printf does but I wouldn't expect quality code there either...


There is no fixed limit. Some systems have only few bytes of stack (maybe 128 or 256), others have mebibytes of them. Some have heap, which is then used by functions, others do not. Some internal functions might use static memory, other implementations don't.


Typically the arguments just go on the call stack, so in a single threaded application, your main limitation is the size of the stack. On Posix, you can use getrlimits() to discover that size, or the Bash built-in ulimit.

A more interesting question might be how much memory printf has for the resulting string, and whether or not it has to perform allocations.


That depends on the implementation and the setup (OS, settings, etc.). Generally, local variables and arguments are stored on a stack, and how big such a stack is depends on the platform, personal settings, etc.

If is generally not good to store large values on the stack. If your function calls other functions, they also need stack space, etc.

And not every implementation might even use a stack, or it may be pretty small.

Just don't store too large values in the local frame.


printf is not required to store anything. And it can be implemented this way. Get argument and print it to output with formatting.

0

精彩评论

暂无评论...
验证码 换一张
取 消