Nik
Nik

Reputation: 695

Internals of printf

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>

#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;

}

Upvotes: 3

Views: 981

Answers (6)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215577

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...

Upvotes: 0

c-smile
c-smile

Reputation: 27470

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

Upvotes: 0

Rudy Velthuis
Rudy Velthuis

Reputation: 28846

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.

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477580

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.

Upvotes: 0

glglgl
glglgl

Reputation: 91149

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.

Upvotes: 0

paulsm4
paulsm4

Reputation: 121869

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" ;-)

Upvotes: 1

Related Questions