简体   繁体   中英

How is a program with many functions stored in a text segment?

Let us say I have a simple c program written to compute the result of a number raised to a power of 2. This is handled by a function called pow(a,n) . The main function just takes in a user input, calls this function with the number and prints the returned result. After I compile and run this program, I think the entire function would be in the text segment in physical memory. Is this correct? When does the stack come into the picture here?

When does the stack come into the picture here?

Before calling this library function pow(a,n) , you declare a variable inside your main function isn't, those variable is allocated on the stack.

text segment in physical memory. Is this correct?

This is irrelevant because of the technique called Virtual Memory, which makes your program feel it has access to address on RAM even your program data/text etc., is present somewhere.

Compiled program will be stored in the text segment from where the instruction are fetched for execution. Address of the function will be know to process( program under execution ) as it is globally declared in a separate table called symbol table .

The stack it C comes in to picture a function call is made. It needs its own place to store the local variables (Frame pointer and passed parameters to the function and so on).

Mostly stack will be used for two purpose.

  1. For storing local variables. say a, b in main()

  2. Call stack.

     int main() { int a = 5, b = 10; //Stored in Stack pow(a,b); //stack role - Call stack for returning control to main function } void pow(int x, int y) { int v; //stack involved here too. ..... } 

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