printf in C doesn't occupy memory? -


does printf occupy memory in stack?

printf("hello world"); 

does "hello world" have constant address?

please me understand.

edit:

is argument passing printf stored in local pointer variable. if use array store 50 string literals takes stack memory if ii use printf doesn't take memory - heard. don't know how printf doesn't take memory array declared.

please me understand!

it depends on platform's calling convention , how standard library implemented.

for example, take following program:

#include <stdio.h>  int main(void) {   printf("hello, world\n");   return 0; } 

and following command line compile it:

gcc -s -std=c99 -pedantic -wall -werror syscall.c 

on 32-bit red hat box (i686) using gcc 2.96, following machine code:

       1         .file   "syscall.c"       2         .version        "01.01"       3 gcc2_compiled.:       4                 .section        .rodata       5 .lc0:       6         .string "hello, world\n"       7 .text       8         .align 4       9 .globl main      10         .type    main,@function      11 main:      12         pushl   %ebp      13         movl    %esp, %ebp      14         subl    $8, %esp      15         subl    $12, %esp      16         pushl   $.lc0      17         call    printf      18         addl    $16, %esp      19         movl    $0, %eax      20         leave      21         ret      22 .lfe1:      23         .size    main,.lfe1-main      24         .ident  "gcc: (gnu) 2.96 20000731 (red hat linux 7.2 2.96-112.7.2)" 

line 16 pushes address of string literal onto stack, , printf called.

here's same code, compiled same way, on 64-bit sles 10 box (x86_64) using gcc 4.1.2:

        1         .file   "syscall.c"       2         .section        .rodata       3 .lc0:       4         .string "hello, world"       5         .text       6 .globl main       7         .type   main, @function       8 main:       9 .lfb2:      10         pushq   %rbp      11 .lcfi0:      12         movq    %rsp, %rbp      13 .lcfi1:      14         movl    $.lc0, %edi      15         call    puts      16         movl    $0, %eax      17         leave      18         ret ;; ;; additional content not included ;; 

in case, line 14 writes address of string literal register (%edi) instead of pushing onto stack. note version of gcc smart enough realize since i'm passing single argument of type char *, can substitute call puts.

in either case you're creating new stack frame when make call; difference what's in stack frame. on red hat box, include address of string literal; on sles 10 box, won't.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -