Thread: memory structure

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    memory structure

    Hi there,
    I have been reading the article shared in link Memory Layout of C Programs - GeeksforGeeks, but I'm still having trouble understanding the memory structure in C. From what I gathered, there are four segments: the text where the program is stored, the data segment for global variables, the stack for local variables, and the heap for dynamic memory.

    I don't use Linux operating system. However, I'm unsure about where other variables like static global variables, static local variables, and external variables, pointer inside function are stored in this memory structure?

    Could you help clarify this for me?
    Last edited by Salem; 01-27-2024 at 08:03 AM. Reason: Font size abuse

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Kittu20 View Post
    Hi there,
    I have been reading the article shared in link Memory Layout of C Programs - GeeksforGeeks, but I'm still having trouble understanding the memory structure in C. From what I gathered, there are four segments: the text where the program is stored, the data segment for global variables, the stack for local variables, and the heap for dynamic memory.

    I don't use Linux operating system. However, I'm unsure about where other variables like static global variables, static local variables, and external variables, pointer inside function are stored in this memory structure?

    Could you help clarify this for me?
    That article is pretty clear, what is not clear for you?

    All static variables, both local and global, along with all global variables are located in the data segments. Static local variables are only accessible in the function where they were defined.

    All non static local variables are located on the stack, in the stack frame for the function call. Pointers are just local or global variables.

    See this Wikipedia article for detailed information.

    You need to study a good up to date book on the C Programming Language to learn more about this and all other features of the C Language.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by rstanley View Post
    That article is pretty clear, what is not clear for you?
    I noticed there wasn't any information about where extern variables are store. Could you please clarify where extern variables are stored in this memory structure?

    Additionally, each function has its own address, but it's not clear where these function addresses are stored.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Kittu20 View Post
    I noticed there wasn't any information about where extern variables are store. Could you please clarify where extern variables are stored in this memory structure?

    Additionally, each function has its own address, but it's not clear where these function addresses are stored.
    Code:
    #include <stdio.h>
    
    int uvar;             // Unitialized global variable, stored in the bss data segment
    int ivar = 10;        // Initialized global variable, stored in the global data segment
    static int count = 0; // Static stored in the global data segment.  Should ALWAYS be initialized!
    
    int foo(void)
    {
      int lvar = 0;         // Created each time the function is called.  ALWAYS Initialize locals!!!
      static int total = 0; // Created in the global data segment, but only accessible in the function
                            // where created.  Retains the current value across multiple function calls
      return 0;
    }
    
    int main(void)
    {
      int retval = 0;  // Local variable
    
      retval = foo();
    
      return 0;
    }
    All non-static global variables are extern, or simply available to all functions.
    Code:
    extern int var;
    is a "declaration" only, and does not define any data space. This statement is normally in a header file #included into each .c file where it needs to be seen and used.
    Somewhere in one of the .c files, and only one of the .c files, it is defined as:
    Code:
    int var = 0;
    The linker resolves all declarations, definitions, and uses of a extern variable.

    Functions are located in the code segment.

    Write a simple program to use and explore these concepts.

    Once again, please study an up to date book on the language!
    Last edited by rstanley; 01-27-2024 at 10:28 AM.

  5. #5
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    I can write code using different types of variables and access them. I am using the MinGW GCC compiler on Windows 11. However, I'm still curious about where variables like const and volatile would be stored in memory.

    Unfortunately, I'm not sure how to check this on my setup.

    Any guidance on how to check in my setup

    Thanks!

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    "const" and "volatile" are NOT data types!

    The keyword volatile should not be used, especially for a beginning programmer, that has not studied a good book on the C Language!

    const prevents a variable from being altered in a program, and is commonly used in parameters to functions.

    The following code should produce this fatal compile error:
    "increment of read-only variable ‘var"
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      const int var = 10;  // var must be initialized and cannot be altered!
    
      var++;               // This should cause a fatal compile error
    
      printf("The value of var: %d\n", var);
    
      return 0;
    }
    Look at this article on the C data types and modifiers!

    We are NOT HERE to teach you the C Programming Language! That is what a course from a qualified instructor, or through a good up to date book on the entire language!!!

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Compilation unit 1:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // tell this compilation unit about the existence of function f2
    // in another comp. unit
    void f2();
    
    int n = 42;    // extern global variable defined in this comp. unit
    
    void f1(int p1) {
        static int x = 123;
        int b = 99;
        if (p1 == 0)
            printf("  f1:   x: %p (%d)\n", (void*)&x, x);
        else {
            printf("  f1:  p1: %p (%d)\n", (void*)&p1, p1);
            printf("  f1:   b: %p (%d)\n", (void*)&b, b);
        }
    }
    
    int main() {
        int a = 9;
    
        printf("text segment:\n");
        printf("  main: %p\n", (void*)main);
        printf("  f1:   %p\n", (void*)f1);
        printf("  f2:   %p\n", (void*)f2);
    
        printf("data segment:\n");
        printf("  main: n: %p (%d)\n", (void*)&n, n);
        f1(0);
        f2(0);
    
        int *p = malloc(sizeof *p);
        *p = 9999;
    
        printf("stack:\n");
        printf("  main: a: %p (%d)\n", (void*)&a, a);
        // pointer itself is stored on the stack;
        // it's value is the heap address that malloc returned
        printf("  main: p: %p (%p)\n", (void*)&p, (void*)p);
        f1(1);
        f2(2);
    
        printf("heap:\n");
        printf("  main: p: %p (%d)\n", (void*)p, *p);
    
        free(p);
    
        return 0;
    }
    Compilation unit 2:
    Code:
    #include <stdio.h>
    
    // tell this compilation unit about the existence
    // of global variable n in another comp. unit
    extern int n;
    
    void f2(int p2) {
        int c = 999;
        if (p2 == 0)
            printf("  f2:   n: %p (%d)\n", (void*)&n, n);
        else {
            printf("  f2:  p2: %p (%d)\n", (void*)&p2, p2);
            printf("  f2:   c: %p (%d)\n", (void*)&c, c);
        }
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by rstanley View Post
    "const" and "volatile" are NOT data types!
    We are NOT HERE to teach you the C Programming Language! That is what a course from a qualified instructor, or through a good up to date book on the entire language!!!
    My main concern when writing variables in code is understanding where they are stored in memory and why they are stored in specific areas as mentioned in link . Given that I'm using MinGW GCC on Windows 11, I'm interested to know about how I can check where variables are stored in my setup

    main.c
    Code:
    #include<stdio.h>
    
    int Global_Variable;
    int Static_Global_Variable;
    
    
    extern int x;
    
    
    void foo()
    {
        int Local_Variable = 20;
        int Static_Local_Variable = 1;
        Static_Local_Variable++;
        printf(" Local_Variable  = %d \n", Local_Variable);
        printf(" Static_Local_Variable  = %d \n", Static_Local_Variable);
    }
    int main ()
    {
        Global_Variable = 11;
        Static_Global_Variable = 12;
        foo();
        
        const int ConstVariable = 12;
        
        printf(" Global_Variable = %d \n", Global_Variable);
        printf(" Static_Global_Variable  = %d \n", Static_Global_Variable);
        
        printf(" ConstVariable = %d \n", ConstVariable);
        
        printf(" x extern variable = %d \n", x);
            
        return 0;
    
    }


    file1.c
    Code:
    int x = 100;


    Output
    Code:
     Local_Variable  = 20 Static_Local_Variable  = 2
     Global_Variable = 11
     Static_Global_Variable  = 12
     ConstVariable = 12
     x extern variable = 100

    Last edited by Kittu20; 01-27-2024 at 01:14 PM.

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    You should have all the information you need to answer these questions. Visit your library and borrow a good book on the C Programming language, if you don't want to buy one.

    Look at the correction of your code:
    Code:
    void foo()
    {
        int Local_Variable = 20;
        static int Static_Local_Variable = 1;
        Static_Local_Variable++;
        printf(" Local_Variable  = %d \n", Local_Variable);
        printf(" Static_Local_Variable  = %d \n", Static_Local_Variable);
    }
    Otherwise, Static_Local_Variable is not static, just a normal local variable.

  10. #10
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by rstanley View Post
    You should have all the information you need to answer these questions.
    Otherwise, Static_Local_Variable is not static, just a normal local variable.
    sorry it was my mistake

    Code:
    #include<stdio.h>
    
    int Global_Variable;
    int Static_Global_Variable;
    
    
    extern int x;
    
    
    void foo()
    {
        int Local_Variable = 20;
        static int Static_Local_Variable = 1;
        Static_Local_Variable++;
        printf(" Local_Variable  = %d \n", Local_Variable);
        printf(" Static_Local_Variable  = %d \n", Static_Local_Variable);
    }
    int main ()
    {
        Global_Variable = 11;
        Static_Global_Variable = 12;
        foo();
        
        const int ConstVariable = 12;
        
        printf(" Global_Variable = %d \n", Global_Variable);
        printf(" Static_Global_Variable  = %d \n", Static_Global_Variable);
        
        printf(" ConstVariable = %d \n", ConstVariable);
        
        printf(" x extern variable = %d \n", x);
            
        return 0;
    }
    I asked one question about where does the address of function store in c memory layout because i don't get clarifications from book and other material i have available. can you please explain for me?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I asked one question about where does the address of function store in c memory layout because i don't get clarifications from book and other material i have available
    The first paragraph of the link you originally posted.
    1. Text Segment: A text segment, also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions.
    As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it.

    Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by rstanley View Post
    All non static local variables are located on the stack, in the stack frame for the function call. Pointers are just local or global variables.
    This is not true. Non static local variables can be located on the stack, but the compiler tries to keep them in registers, unless NO optiomizations are used...

  13. #13
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    > I asked one question about where does the address of function store in c memory layout because i don't get clarifications from book and other material i have available
    The first paragraph of the link you originally posted.
    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() functions 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;
    }
    Last edited by Kittu20; 01-28-2024 at 07:57 AM.

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