Thread: memory structure

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #14
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Kittu20 View Post
    sorry but i don't find in paragraph that show where address of function store in which area

    imagine we have code where does foo() and fun will store and in which reason of memory

    Code:
     #include<stdio.h>
    
    void foo()
    {
        printf("Welcome \n");
    }
    
    
    static void fun ()
    {
        printf("Hello \n");
    }
    int main ()
    {
        foo();
        fun ();
        return 0;
    }
    static here have a different meaning. For functions it means the identifier fun won't be exported (the function is local to the module).

    main and foo are exported (made extern).

    In this example all bytes for the functions are encoded in .text section.

    Why this is useful? Take a look at this:
    Code:
    // test1.c
    #include <stdio.h>
    
    extern void g( void );
    
    static void f( void ) { puts( "I'm f() from test1.c" ); }
    
    int main( void ) { f(); g(); return 0; }
    Code:
    // test2.c
    #include <stdio.h>
    
    static void f( void ) { puts( "I'm f() from test2.c" ); }
    
    void g( void ) { f(); } // calls the module local f().
    Code:
    $ cc -O2 -c *.c
    $ cc -s -o test test1.o test2.o
    $ ./test
    I'm f() from test1.c
    I'm f() from test2.c
    Different functions with the same name in different modules.

    PS: The functions declarations with () are wrong, in C. Modern compilers allows it, but the correct way to declare is:
    Code:
    int main( void ) // void is necessary!
    {
      ...
    }
    Otherwise, this code is C++, not C.
    Last edited by flp1969; 01-28-2024 at 08:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how much memory structure take in code
    By abhi143 in forum C Programming
    Replies: 2
    Last Post: 11-30-2019, 12:33 AM
  2. Replies: 4
    Last Post: 07-19-2015, 05:51 PM
  3. I need your Help, structure in memory
    By Serj in forum C Programming
    Replies: 4
    Last Post: 12-09-2011, 12:15 PM
  4. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  5. Allocating Memory for a Structure
    By surfxtc79 in forum C Programming
    Replies: 4
    Last Post: 06-05-2003, 11:40 AM

Tags for this Thread