Click here to Skip to main content
15,889,833 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I found that static memory can be allocated at compile time, as the size the object/variable going to take is predefined.

My question is "how it[compiler] knows which memory location will be assign to the application by OS,[while launching the apps.] so that all the static [int x] variable could allocate memory"?

e.g.
executable name: run.exe
==========================
code
========
void main()
{
int x;//Say 'x' assigned to->0xFFAB12 memory location while compiling
}

now restart the machine

and run run.exe without compiling again

how it knows 0xFFAB12 address again will fall under the memory area alloted by OS to run.exe?


if I have some conceptual problem, please let me know.

thanks in advance
Posted

For this answer i have compiled the information from different sources but mainly from Q&A of Stackoverflow.com.

A process has 5 different areas of memory allocated

1. Code - text segment
2. Initialized data – .data segment
3. Uninitialized data – .bss segment
4. Heap
5. Stack

+--------------------------+
| |
| command line |
| arguments |
| (argc and argv[]) |
| |
+--------------------------+
| Stack |
| (grows-downwards) |
| |
| |
| |
| F R E E |
| S P A C E |
| |
| |
| |
| |
| (grows upwards) Heap |
+--------------------------+
| |
| Initialized data |
| segment |
| |
+--------------------------+
| |
| Initialized to |
| Zero (BSS) |
| |
+--------------------------+
| |
| Program Code |
| |
+--------------------------+


In your case the answer is -
void main()
{
int x;\\on the stack, destroyed when main() returns
static int i;  \\stored in BSS
}


More general sample (see What is stored on heap and what is stored on stack?[^]) -
char * str = "Text line 1";  /* 1 */
char * buf0 ;                         /* 2 */

int main(){
    char * str2 = "Text line 2" ;  /* 3 */
    static char * str3 = str;         /* 4 */
    char * buf1 ;                     /* 5 */
    buf0 = malloc(BUFSIZ);            /* 6 */
    buf1 = malloc(BUFSIZ);            

    return 0;
} 


1.This is neither allocated on the stack NOR on the heap. Instead it's allocated as static data, and put into it's own memory segment on most modern machines. The actual string is also being allocated as static data, and put into a read-only segment in right-thinking machines.

2. Is simply a static allocated pointer; room for one address, in static data.

3. Has the pointer allocated on the stack, and will be effectively deallocated when main returns. The string, since it's a constant, is allocated in static data space along with the other strings.

4. Is actually allocated exactly like at 2. The static keyword tells you that it's not to be allocated on the stack.

5. But buf1 is on the stack, and

6. The malloc'ed buffer space is on the heap.
 
Share this answer
 
v4
 
Share this answer
 
v2
Comments
shankha2010 23-May-12 8:32am    
http://www.arjay.bc.ca/Modula-2/Text/Ch12/Ch12.4.html
thanks for this link :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900