简体   繁体   中英

Memory consumption by printf( )

Does displaying a simple statement in C (or C++) occupy some memory? For example,

//in C
printf("\nHello World");
//in C++
cout<<"Hello World" ;

and, will it make a difference if I attach some value of a variable to be displayed in the same statement? For example,

printf("Value is %d" , var) ; 

Code occupies memory. String literals occupy memory. Function calls (usually) use some stack.

Generally speaking I don't think printf should need to perform any dynamic memory allocations in order to work. But although (I believe) it's possible to avoid it I don't think they're forbidden from doing so. The same goes for cout << when outputting the types that have built-in support. If it ends up calling a user-defined overload then that can use whatever memory it likes.

Posix lists ENOMEM as a possible error for printf but not for snprintf . This suggests that on Posix systems (which of course is not all C implementations) the output might dynamically allocate memory, but the formatting will not.

Does displaying a simple statement in C(or C++) occupies some memory?

Yes, of course. The string constant has to be stored somewhere, usually in a read-only segment of memory. The printf and cout facilities also take up space.

will it make a difference if i attach some value of a variable to be displayed in the same statement?

Yes. The parameters have to be stored somewhere, usually on the stack, so the memory used by the parameters will be returned after the printf or cout call ends. Also the call itself probably generates a few more instructions in the calling routine to push the parameters on the stack.

Well, yes. The characters in the string requires memory, so these objects are placed in the global memory, but not in the process heap. When the function is called, its arguments passes trough stack, and then the call happens.

push        offset string "hello, world"
call        dword ptr printf

We have its address, calculated by the offset directive. So it use sizeof(uintptr_t) bytes of stack memory.

[UPD.] This code:

int val = 5;
printf("val = %d", val);

Disassembles to:

mov         eax, dword ptr[val]  
push        eax  
push        offset string "val = %d"
call        dword ptr printf

So it use 2 * sizeof(uintptr_t) bytes of stack memory.

Also note that printf and cout are buffered write. Chances are if the buffer is not yet filled for output and your program quits which triggers a closure of output stream port, the print string will never show.

If you mean heap memory, then the answer is, yes (at least, in general).

We produced some middleware to run on a variety of game consoles, with GCC, visual studio and Metrowerks compilers. A common constraint in the gaming industry is that you may need to allocate to custom heaps -- so we had to ensure that our middleware library made no heap allocations (other than to a heap allocator explicitly provided to us). So ensure this, we had to:

  • drop calls to print, vsprintf, ...
  • drop the use of C++ streams

both of these sets of calls (in general) use the heap. printf (in general) requires the heap because of the use of format strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM